how to implement a long click listener on a listview


I want to add OnLongClickListener on my list view. Whenever the user long press the item in list some action should be performed, But my code does not catch this listener. Please let me know where I am going wrong. The similar code works for setOnItemClickListener very well.

Here is the code :

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View v,
                    int index, long arg3) {
                // TODO Auto-generated method stub
                 Log.d("in onLongClick");
                 String str=listView.getItemAtPosition(index).toString();

                 Log.d("long click : " +str);
                return true;

This question is tagged with android android-listview onlongclicklistener

~ Asked on 2012-01-13 06:19:25

The Best Answer is


You have to set setOnItemLongClickListener() in the ListView:

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int pos, long id) {
                // TODO Auto-generated method stub

                Log.v("long clicked","pos: " + pos);

                return true;

The XML for each item in the list (should you use a custom XML) must have android:longClickable="true" as well (or you can use the convenience method lv.setLongClickable(true);). This way you can have a list with only some items responding to longclick.

Hope this will help you.

~ Answered on 2012-01-13 06:36:11


If your ListView row item refers to a separate XML file, be sure to add android:longClickable="true" to that layout file in addition to setting setOnItemLongClickListener() to your ListView.

~ Answered on 2013-05-29 09:02:39

Most Viewed Questions: