Android ListActivity Example

If there is only one ListView in your android Activity class, you can make it simpler. You just need to make your activity extends the android.app.ListActivity, then you can manipulate the ListView object in it. This article will show you how to create a single ListView activity.

1. Create Activity Extends ListActivity.

  1. Although ListActivity has an inner ListView object. But it still needs two layout XML files. One is used for ListActivity, the other is used for ListActivity‘s inner ListView‘s item.
  2. So for the above reasons, you can create an empty activity first in android studio. Please read the article How To Add Activity In Android Studio to learn more if you do not know.
  3. In this example, we create an activity named as ListActivityExampleActivity and create it’s layout XML file named activity_list_activity_example.xml. The layout XML file is saved in the app/res/layout folder.
  4. After that, make ListActivityExampleActivity extends ListActivity. Then you can override it’s onListItemClick method. You can put java code in this method to respond to user click-list-item events.
  5. Please Note: ListActivity has a setListAdapter method, this method can set a data adapter to the ListView.
  6. ListActivityExampleActivity.java
    package com.dev2qa.example;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.support.v7.app.AlertDialog;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ListActivityExampleActivity extends ListActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Set the activity layout xml file.
            setContentView(R.layout.activity_list_activity_example);
    
            // Create a list data which will be displayed in inner ListView.
            List<String> listData = new ArrayList<String>();
            listData.add("Audi");
            listData.add("Benz");
            listData.add("BMW");
            listData.add("Ford");
            listData.add("Honda");
            listData.add("Toyoto");
    
            // Create the ArrayAdapter use the item row layout and the list data.
            ArrayAdapter<String> listDataAdapter = new ArrayAdapter<String>(this, R.layout.activity_list_activity_example_row, R.id.listRowTextView, listData);
    
            // Set this adapter to inner ListView object.
            this.setListAdapter(listDataAdapter);
        }
    
        // When user click list item, this method will be invoked.
        @Override
        protected void onListItemClick(ListView listView, View v, int position, long id) {
            // Get the list data adapter.
            ListAdapter listAdapter = listView.getAdapter();
            // Get user selected item object.
            Object selectItemObj = listAdapter.getItem(position);
            String itemText = (String)selectItemObj;
    
            // Create an AlertDialog to show.
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setMessage(itemText);
            alertDialog.show();
        }
    }

2. Create The ListActivity Layout XML File.

  1. In above java code, ListActivity still need to call setContentView() method to set it’s layout xml file.
    setContentView(R.layout.activity_list_activity_example);
  2. But please note, the layout XML file must have one ListView object whose id is android:list. Otherwise, an exception will be thrown like below.
    Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
  3. Our demo layout XML file is simple, it has a ListView and a TextView.
  4. The TextView behaves as the ListActivity‘s title because ListActivity does not show the title as default.
  5. activity_list_activity_example.xml
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="dev2qa.com --- ListActivity Example"
            android:textSize="25dp"
            android:background="@color/colorBlue"
            android:textColor="@android:color/white"/>
        <ListView
            android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:headerDividersEnabled="true"/>
    
    </LinearLayout>

3. Create ListView Item Layout XML File.

  1. This example uses ArrayAdapter as the data adapter.
  2. Please note below the java code. The ArrayAdapter‘s second parameter is just the item layout XML file name. And the third parameter is just the TextView‘s id in the item layout XML file.
    // Create the ArrayAdapter use the item row layout and the list data.
    ArrayAdapter<String> listDataAdapter = new ArrayAdapter<String>(this, R.layout.activity_list_activity_example_row, R.id.listRowTextView, listData);
  3. So we need to create an item layout XML file follow the below steps. And it’s name is activity_list_activity_example_row.xml. It is also saved in the app/res/layout folder.
  4. Right-click app / res / layout folder.
  5. Click New —> Layout resource file in the popup menu.
  6. Input the name activity_list_activity_example_row.
  7. activity_list_activity_example_row.xml
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/listRowTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="@android:color/holo_red_light"
            android:textStyle="bold"
            android:background="@android:color/holo_blue_bright"
            android:minHeight="50dp" />
    
    </LinearLayout>
  8. Run the above example, you can see below demo video. If you want to learn how to use SimpleAdapter, BaseAdapter to customize the ListView in ListActivity, please read the article Android ListView Example

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/5oewy77QCIw

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.