ListView with ListActivity in Android

June 20, 2012

In this article, we are creating a listview widget using ListActivity. In this demo, we are not using any layoutfile instead we make use the default layout of the listview.

1. Create an Android project namely ListViewWithListActivity

New Android Project

Figure 1 : New Android Project

2. Select a build target for this application

Select build target

Figure 2: Select build target


3. Enter application details

Application Details

Figure 3 : Application Details

4. Open and edit the file /src/in/wptrafficanalyzer/listviewwithlistactivity/

package in.wptrafficanalyzer.listviewwithlistactivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    String[] countries = new String[] {
    "Sri Lanka",
    "North Korea",
    "South Korea",

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        // Declaring ArrayAdapter for the default listview
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,countries);

        // Setting ArrayAdapter for the default listview

        // Defining ItemClick event Listener
        OnItemClickListener listener = new OnItemClickListener() {

            // This method will be triggered when an item in the listview is clicked ( or touched )
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
                Toast.makeText(getBaseContext(), "You selected : " + countries[position], Toast.LENGTH_SHORT).show();

        // Setting an ItemClickEvent Listener for the listview
        // In this example we are making use the default listview


5. Run the application

Application in Execution

Figure 4: Application in Execution

6. Download the source code

7. Reference

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: ,

Leave a Reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner