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 no mean. In this tutorial, we will discuss how to handle the click event on a listview item.

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

Here is the brief of creating a list view with the 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 the particular item when you click on any item of the ListView.

  1. Create the 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 an array as a Datasource. Create string array inside res -> values-> string.xml as below.

4. Create layout for the single item an of a 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.

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

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

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

ListItemClick
ListItemClick

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