Android ListView Item Click

In the tip:, you learnt how to customize a ListView in Android to display both text and images. If you click or touch an item of the list, there is no response. To make the list response when its item touched, you need to set a item click listener to the list with the setOnItemClickListener method. The click listener is a class that implements the OnItemClickListener interface. You must override the onItemClick method to respond to the item click event of the list. In the example code below, the ItemList class implements the OnItemClickListener interface. The onItemClick method is overridden to display the text part of the clicked item using Toast. The ViewGroup viewg is referred to the clicked item of the list. As an item of the list contains two elements: text and image, the ViewGroup viewg contains the two elements as well. In the ViewGroup, you can access all its elements with the findViewById method.

      private class ItemList implements OnItemClickListener{
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
     ViewGroup viewg=(ViewGroup)view;
     TextView textv=(TextView)viewg.findViewById(;
     Toast.makeText(context, textv.getText().toString(), Toast.LENGTH_LONG).show();
After you created the ItemList class, to make the ListView react when its item is touched, you need to create an instance of the ItemList class and set the instance to the ListView by using the setOnItemClickListener method.

ListView list=(ListView)findViewById(;
list.setOnItemClickListener(new ItemList());


Posted by: Dara | post date: 05-11-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.