Sunday, 1 September 2013

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.


No comments:

Post a Comment