ListView is a View Group that creates and manages a vertical list of Views, displaying them as rows within a list. The simplest List View displays the toString value of each object in an array, using a Text View for each item. This tutorial shows how to create a list view and launch a new activity on selecting each item in the list.
Sample Output:

Start by creating a new project and extend the main activity java class from ListActivity.
Create a new XML file named list_view.xml in res->layout folder and insert the following.
|
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="15dp" android:textSize="20sp"> </TextView> |
We need to store all list items as string resources file. Create three new Android XML files comapnies_array.xml, apple_products.xml, and google_products.xml in res->values folder.
comapnies_array.xml
|
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="companies_array"> <item>Apple</item> <item>Google</item> </string-array> </resources> |
apple_products.xml
|
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="apple_products"> <item>iPhone</item> <item>iPad</item> <item>iPode touch</item> <item>Apple TV</item> <item>MacBook Pro</item> </string-array> </resources> |
google_products.xml
|
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="google_products"> <item>Gmail</item> <item>Android</item> <item>G Drive</item> <item>Google plus</item> <item>Google Docs</item> </string-array> </resources> |
Now the main activity file.
ListViewActivity.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.cw.listview; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; public class ListViewActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] companies = getResources().getStringArray(R.array.companies_array); setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view, companies)); ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // When clicked, show the products of the selected company switch(position){ case 0: Intent intent = new Intent(getApplicationContext(), AppleProducts.class); startActivity(intent); break; case 1: Intent intent1 = new Intent(getApplicationContext(), GoogleProducts.class); startActivity(intent1); break; } } }); } } |
AppleProducts.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.cw.listview; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class AppleProducts extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] apple_pro = getResources().getStringArray(R.array.apple_products); setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view, apple_pro)); ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // When clicked, show a toast with the TextView text Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); } }); } } |
GoogleProducts.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.cw.listview; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class GoogleProducts extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] google_pro = getResources().getStringArray(R.array.google_products); setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view, google_pro)); ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // When clicked, show a toast with the TextView text Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); } }); } } |
We can directly use an string array by defining as below:
|
1 |
static final String[] google_pro = new String[] { "Gmail", "Android", "G Drive", "Google Plus", "Google Docs" }; |
and use it as
|
1 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view, google_pro)); |
Edit the AndroidManifest.xml as below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cw.listview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".ListViewActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".AppleProducts" android:label="Products by Apple"></activity> <activity android:name=".GoogleProducts" android:label="Products by Google"></activity> </application> </manifest> |
List views can also be made dynamic by getting the data from a url in xml format. You have to use XML Parsing for that. Upcoming posts will cover all that. Stay tuned.







