Android ListView Tutorial

ListView is a ViewGroup that displays a list of scrollable items. The list items are automatically inserted into the list using a ListAdapter. This tutorial is all about creating ListView and showing a toast message on selecting single list item.

Here is our step by step tutorial-

1. Create a new project by going to File ⇒ New Android Project.
    Project Name : SimpleListView
    Package : com.sunil.simplelistview
    Main Activity :
    Build Target : Android 2.2

2. Once the project is created open your main activity ( and extend the class from ListActivity.

public class SimpleListViewActivity extends ListActivity {

3. Now we need a string resources file to store all list items. So create an XML file under res/values/  folder and name it as list_data.xml and type the following code.

<?xml version="1.0" encoding="utf-8"?>
    <string-array name="social_sites">
        <item> LinkedIn</item>
        <item>Google Plus+</item>

4. Now create the layout for each item that will be placed in the ListView. Create an XML file named list_item.xml and save it inside the res/layout/  folder.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=""
        android:textStyle="bold" >

5. Open your main activity ( and type the following code. 

package com.sunil.simplelistview;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class SimpleListViewActivity extends ListActivity {
    // defining array to store string resources
    String[] social_sites;
        public void onCreate(Bundle savedInstanceState) {
            // storing string resources into Array
            social_sites = getResources().getStringArray(R.array.social_sites);
            // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,, social_sites));

    protected void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(this, "You have selected : "+social_sites[position], Toast.LENGTH_SHORT).show();

6. Now run your project. You will see the listview with list of array items. When a list item is selected you will see a toast message.

Android ListViewListView Toast

That's it ! You can also download the source code of this tutorial.

Download Code


dharmendar rao said...


Latifah Yang said...

I have read your further tutorial blog also about android.I gained many things from your sites.

Kaushal Fauzdar said...

Thank you sir :)

Post a Comment

Recent Posts

Site Stats

Our Apps

Get it on Google Play

©2012-2015 AndroidAspect - All rights reserved | Powered by Blogger

Creative Commons License