Android: ListView in ListActivity

3 September 2012 By Nithya Vasudevan 15,737 views One Comment
7 Flares Twitter 0 Facebook 0 Google+ 7 7 Flares ×

Project Description

  • This Android tutorial shows how to use ListView in ListActivity. ListActivity has a default layout that consists of a single ListView object. If the layout contains only ListView, then it is not required to assign a layout to ListActivity via the setContentView() method.
  • Here, we define string array programatically and use ArrayAdapter to bind the data items.
  • Here, we are going to implement OnItemClickListener event listener which calls onItemClick() callback method where we retrieve and display the item selected by the user.

Environment Used

  • JDK 6 (Java SE 6)
  • Eclipse Indigo IDE for Java EE Developers (3.7.1)
  • Android SDK 4.0.3 / 4.1 Jelly Bean
  • Android Development Tools (ADT) Plugin for Eclipse (ADT version 20.0.0)
  • Refer this link to setup the Android development environment


Create Android Project

  • Create a new Android Project and name it as “ListViewDemo“.
  • Enter the package name as ““.
  • Enter the ListActivity name as “ListViewInListActivity“.
  • Click Finish.


Open res/values/string.xml and replace it with following content.

<?xml version="1.0" encoding="utf-8"?>
    <string name="hello">Hello World, ListViewInActivity!</string>
    <string name="app_name">ListViewDemo</string>

XML layout file

Create a new XML file named “list_item.xml” in res/layout folder to define a layout for each row in the ListView. Copy the following code.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=""
    android:padding="10dp" />


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

        android:label="@string/app_name" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

Activity class

Make the class extend ListActivity instead of Activity.


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 ListViewInListActivity extends ListActivity 
		implements OnItemClickListener{
    ListView listView;
    static final String[] SPORTS = {"Shuttle Badminton", "Tennis", "FootBall",
				"Basket Ball","Table Tennis", "Chess","Hockey"};
    public void onCreate(Bundle savedInstanceState) {
        listView = getListView();
        //Own row layout
        listView.setAdapter(new ArrayAdapter<String>(this, 
					R.layout.list_item, SPORTS));        
    public void onItemClick(AdapterView<?> adapter, View view, 
  				int position, long id) {
	Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
  • We have used ListView inside ListActivity. To retrieve the ListView object we call the method getListView().
  • For simplicity, we hard-coded a string array instead of using string array defined in external resource (res/values/strings.xml) and used it in ArrayAdapter to load the entries.


Run your application

Project Folder Structure

The complete folder structure of this example is shown below.

Tags: , , , , , , , , , ,

  • Omi

    I am working in android. I took reference of your example in my example where i populate value from json.I want to display selected value from one activity to another activity i did same which you did. but in my case not selected key go to next activity rather its value going to next activity.