Showing posts with label string array. Show all posts
Showing posts with label string array. Show all posts

Sunday, 1 September 2013

HOW TO ADD STRING ARRAY IN ANDROID APPLICATION

YOU CAN STORE YOUR DATA IN STRINGS.XML FILE.YOU CAN ADD YOUR DATA IN THAT FILE BY DECLARING ARRAY.YOU CAN DO THAT BY USING GUI AS WELL OR YOU CAN JUST ADD FOLLOWING CODE IN YOUR STRINGS.XML FILE.

HERE IS HOW YOUR STRINGS.XML FILE LOOKS LIKE BY DEFAULT
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Days</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
</resources>
NOW SUPPOSE WE WANT TO STORE DAYS NAME IN THIS FILE.WE NEED TO WRITE FOLLOWING CODE


<string-array name="days">
        <item >MONDAY</item>
        <item >TUESDAY</item>
        <item >WEDNESDAY</item>
        <item >THURSDAY</item>
        <item >FRIDAY</item>
        <item >SATURDAY</item>
        <item >SUNDAY</item>
    </string-array>

HOW TO ADD LISTVIEW IN ANDROID ACTIVITY

1.CREATE A NEW ANROID APPLICATION PROJECT


2.GIVE A NAME TO YOUR APPLICATION

3.ADD A LISTVIEW FROM COMPOSITE
4.NOW YOUR ACTIVITY_MAIN.XML CODE WILL LOOKE LIKE FOLLOWING AFTER ADDING LISTVIEW
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>


5.NOW WE NEED TO SHOW DATA IN LISTVIEW ITEMS SO FOR THAT WE NEED TO CREATE STRING ARRAY.SO FOR THAT YOU NEED TO OPEN STRING FILE(res/values/strings.xml).IT WILL LOOK LIKE FOLLOWING

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

    <string name="app_name">ListView Demo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

</resources>


6.NOW WE WILL ADD STRING ARRAY IN THAT SO WHERE WE CAN STORE OF LISTVIEW ITEM DATA.

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

    <string name="app_name">ListView Demo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string-array name="COUNTRY">
        
    </string-array>

</resources>

HERE WE HAVE CREATED COUNTRY ARRAY WHERE WE WILL STORE OUR DATA

7.NOW WE WILL ADD OUR DATA INTO LISTVIEW

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

    <string name="app_name">ListView Demo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string-array name="COUNTRY">
        <item >AUSTRALIA</item>
        <item >NEW ZEALAND</item>
        <item >USA</item>
        <item >CANADA</item>
        <item >INDIA</item>
        
    </string-array>

</resources>

AFTER ADDING ALL THE DATA OUR STRINGS.XML FILE WILL LOOK LIKE THIS.SAVE THE PROJECT AND RUN IT.YOU WILL SEE ALL THE COUNTRIES IN A LISTVIEW.