Wednesday, 30 December 2015

Array in Android

23:55

   
Arrays:

Arrays refer to Collection of values or elements.Arrays are known for their capability of direct reference;

(1)String Arrays :
As the name Suggests ,the String array provides an array of strings. such a resource is popularly  used with selection widgets such as
Listview and spinner that need to display  a collection of selectable items to the user.

to define string array syntax is:

<string-array  name="array_name">
<item>text1</item>
<item>text 2</item>
.....
.....
</string>
(2)Integer Arrays:

<integer-array name="OddNumbers">
    <item>1</item>
    <item>3</item>
    <item>5</item>
    <item>7</item>
    <item>9</item>
</integer-array>


  ==========================================================================================================
      String Array example:
 =====================================================


  (1)  write  code for   MainActivity.java :
package checkbox.test.om.com.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        TextView  fruitsView=(TextView)findViewById(R.id.
fruits_view);
        String []  fruitsArray=getResources().getStringArray(R.array.
fruits);
        String str=
"";
       
for(int i=0; i<fruitsArray.length;i++)
        {
            str +=fruitsArray[i]+
"\n";
        }
        fruitsView.setText(str);
    }


}

(2)write code  activity_main.xml  file:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)write code for content_main.xml file:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <TextView

        android:id="@+id/fruits_view"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

</LinearLayout>

(4)write code for strings.xml file :

<resources>

    <string name="app_name">StringArrayApp</string>

    <string name="action_settings">Settings</string>

    <string name="title_activity_string_array_app">StringArrayAppActivity</string>

    <string-array name="fruits">

        <item>Apple</item>

        <item>Mango</item>

        <item>Orange</item>

        <item>Grapes</item>

        <item>Banana</item>

    </string-array>

</resources>



output :

========================================================================
========================================================================

Integer array example:
========================================================================

(1) first   write code for  MainActivity.java :

package checkbox.test.om.com.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        TextView  oddNumsView=(TextView)findViewById(R.id.
oddnums_view);
       
int []  oddNumsArray=getResources().getIntArray(R.array.OddNumbers);
        String str=
"";
       
for(int i=0; i<oddNumsArray.length;i++)
        {
            str +=oddNumsArray[i]+
"\n";
        }
        oddNumsView.setText(str);
    }


}

(2)write code for activity_main.xml  file:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)write  code for content_main.xml file :

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <TextView

        android:id="@+id/oddnums_view"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

</LinearLayout>

(4) write   code   for   strings.xml   file  :
<resources>

    <string name="app_name">InegerArrayApp</string>

    <string name="action_settings">Settings</string>

    <integer-array name="OddNumbers">

        <item>1</item>

        <item>3</item>

        <item>5</item>

        <item>7</item>

        <item>9</item>

    </integer-array>

</resources>



 output :




Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 ANDROID TRAINING AT VISSICOMP BY OM SIR. All rights resevered. Designed by Templateism

Back To Top