SHARE
How to handle ListItem click in Android
How to handle ListItem click in Android
Add it to read later !

How to handle List Item click in Android

Creating a simple ListView in an easy task, you can check my tutorial for simple listview. But list view that does not handle click is of no mean. In this tutorial we will discuss how to handle click event on a listview item.

If you ever have implemented my simple listview tutorial and want to handle click event, you just need to add the below code to the SimpleListViewActivity which is responsible to handle click on a list Item.

ListView listView = getListView();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                  // you can perform your own stub
                String item_selected = ((TextView)view.findViewById(R.id.osName)).getText().toString();/* get the selected row item*/
                Toast.makeText(SampleListViewActivity.this, item_selected + " is at position " + i, Toast.LENGTH_LONG).show(); /*show the selected item and position*/ 
            }
        });

    }

Here is the brief of creating a list view with click on its item. I am going to show you how to handle click on a listview item with a simple Toast that will appear showing item and position of particular item when you click on any item of the ListView.

  1. Create new project in Android Studio as File ->New-> New Project.
  2. Name your project whatever you want and rename MainActivity as SimpleListViewActivity then select Finish.
  3. We want data that will display in list view, here I am using array as a datasource. Create string array inside res -> values-> string.xml as below.
<resources>
    <string name="app_name">ListViewExample</string>
 
    <string-array name="types_of_os">
        <item>Android OS (Google Inc.)</item>
        <item>Bada (Samsung Electronics)</item>
        <item>BlackBerry OS (Research In Motion)</item>
        <item>iPhone OS / iOS (Apple)</item>
        <item>MeeGo OS (Nokia and Intel)</item>
        <item>Palm OS (Garnet OS)</item>
        <item>Windows Mobile (Windows Phone 7)</item>
    </string-array>
</resources>

4. Create layout for single item of list. res » layout » right click » Layout resource file » give a name listview_item.xml  then press OK.

5. Copy the below code to the listview_item.xml.

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/osName"
        android:padding="15dp"
        android:layout_marginTop="10dp"
        android:textSize="18dp"
        android:background="@color/button_material_light"/>

</LinearLayout>

6. Now open SimpleListViewActivity from java package and copy the below text.

package com.buzzmycode.listviewexample;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SimpleListViewActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] mobile_os = getResources().getStringArray(R.array.types_of_os);
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.listview_item, R.id.osName, mobile_os));

        ListView listView = getListView();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                
                String item_selected = ((TextView)view.findViewById(R.id.osName)).getText().toString();
                Toast.makeText(SampleListViewActivity.this, item_selected + " is at position " + i, Toast.LENGTH_LONG).show();
            }
        });
    }
}

7. Finally, Open your AndroidManifest.xml from manifest package and add make changes to your AndroidManifest file as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.buzzmycode.listviewexample">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SampleListViewActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

8. Now run your project you will see the output as shown below.

ListView with click on its Item
ListView with click on its Item

Here we done with Simple ListView with click on its item. I hope you found it helpful. For any query comment below i would love to solve it.