Android ListActivity Example

If there is only one ListView in your android activity, you can make it simpler. You just need make your activity extends 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.

Although ListActivity has an inner ListView object. But it still need two layout xml file. One is used for ListActivity, the other is used for ListActivity’s inner ListView’s item.

So for above reasons, you can create an empty activity first in android studio. Please read article How To Add Activity In Android Studio to learn more if you do not know.

In this example, we create an activity named as ListActivityExampleActivity, and create it’s layout xml file named as activity_list_activity_example.xml. The layout xml file saved in app / res / layout folder.

After that, make ListActivityExampleActivity extends ListActivity. Then you can override it’s onListItemClick method. You can put java code in this method to response user click list item event.

Please Note: ListActivity has a setListAdapter method, this method can set a data adapter to the ListView. 

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.

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);

But please note, the layout xml file must has one ListView whose id is android:list. Otherwise, an exception will be thrown like below.

READ :   Android SeekBar Example

andrlid list activity runtime error

Our demo layout xml file is simple, it has a ListView and a TextView. The TextView behave as the ListActivity’s title, because ListActivity do not show title as default.

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.

This example use ArrayAdapter as the data adapter. Please note below java code. The ArrayAdapter’s second parameter is just the item layout xml file name. And the third parameter is just the TextView 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);

So we need to create a item layout xml file as below. And it’s name is activity_list_activity_example_row.xml. It is also saved in app / res / layout folder.

  1. Right click app / res / layout folder.
  2. Click New —> Layout resource file in popup menu.
  3. Input the name activity_list_activity_example_row.

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>

Run above example, you can see below screen.

android listactivity array adapter example

If you want to learn how to use SimpleAdapter, BaseAdapter to customize the ListView in ListActivity, please read article Android ListView Example

(Visited 1,806 times, 10 visits today)

Leave a Reply

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.