How to handle ListView click in Android

The question:

How do I listen to click event on a ListView?

This is what I have now

ListView list = (ListView)findViewById(;  

When I do the following

list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
   public void onItemSelected(AdapterView parentView, View childView, 
                                                         int position, long id) 

   public void onNothingSelected(AdapterView parentView) {  


That doesn’t seem to do anything on click.
And all those code live within a class that extends Activity.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

On your list view, use setOnItemClickListener

Method 2

Suppose ListView object is lv, do the following-

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

  public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

    Object o = lv.getItemAtPosition(position);
    /* write you handling code like...
    String st = "sdcard/";
    File f = new File(st+o.toString());
    // do whatever u want to do with 'f' File object

Method 3

You need to set the inflated view “Clickable” and “able to listen to click events” in your adapter class getView() method.

convertView = mInflater.inflate(R.layout.list_item_text, null);

and declare the click listener in your ListActivity as follows,

public OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
                 //code to be written to handle the click event

This holds true only when you are customizing the Adapter by extending BaseAdapter.

Refer the ANDROID_SDK/samples/ApiDemos/src/com/example/android/apis/view/ for more details

Method 4

The two answers before mine are correct – you can use OnItemClickListener.

It’s good to note that the difference between OnItemClickListener and OnItemSelectedListener, while sounding subtle, is in fact significant, as item selection and focus are related with the touch mode of your AdapterView.

By default, in touch mode, there is no selection and focus.
You can take a look here for further info on the subject.

Method 5

This solution is really minimalistic and doesn’t mess up your code.

In your list_item.xml (NOT listView!) assign the attribute android:onClick like this:

<RelativeLayout android:onClick="onClickDoSomething">

and then in your activity call this method:

public void onClickDoSomething(View view) {
   // the view is the line you have clicked on

Method 6

You have to use setOnItemClickListener someone said.
The code should be like this:

listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // When clicked, show a toast with the TextView text or do whatever you need.
        Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

Method 7

First, the class must implements the click listenener :

implements OnItemClickListener

Then set a listener to the ListView


And finally, create the clic method:

public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +countries[+ position], Toast.LENGTH_SHORT).show();

you can take a look and download code here

Method 8

Use setOnItemClickListener() api in your activity. Following is the sample.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

public void onItemClick(AdapterView<> parent, View view, int position, long id)
     // your code here.


Method 9

In Kotlin, add a listener to your listView as simple as java

your_listview.setOnItemClickListener { parent, view, position, id ->   

    Toast.makeText(this, position, Toast.LENGTH_SHORT).show()


All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment