how to implement a long click listener on a listview

AndroidAndroid ListviewOnlongclicklistener

Android Problem Overview


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;
			}
}); 
			

Android Solutions


Solution 1 - Android

You have to set setOnItemLongClickListener() in the ListView:

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
			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.

Solution 2 - Android

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.

Solution 3 - Android

or try this code:

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View v,
                    int index, long arg3) {
			
	Toast.makeText(list.this,myList.getItemAtPosition(index).toString(), Toast.LENGTH_LONG).show();
                return false;
            }
}); 

Solution 4 - Android

I think this above code will work on LongClicking the listview, not the individual items.

why not use registerForContextMenu(listView). and then get the callback in OnCreateContextMenu.

For most use cases this will work same.

Solution 5 - Android

In xml add

<ListView android:longClickable="true">

In java file

lv.setLongClickable(true) 

try this setOnItemLongClickListener()

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int pos, long l) {
                //final String category = "Position at : "+pos;
                final String category = ((TextView) view.findViewById(R.id.textView)).getText().toString();
                Toast.makeText(getActivity(),""+category,Toast.LENGTH_LONG).show();
                args = new Bundle();
                args.putString("category", category);
                return false;
            }
        });

Solution 6 - Android

this should work

ListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                           int pos, long id) {
                // TODO Auto-generated method stub

                Toast.makeText(getContext(), "long clicked, "+"pos: " + pos, Toast.LENGTH_LONG).show();

                return true;
            }
        });

also don't forget to in your xml android:longClickable="true" or if you have a custom view add this to your custom view class youCustomView.setLongClickable(true);

here is the output of the code above enter image description here

Solution 7 - Android

I tried most of these answers and they were all failing for TextViews that had autolink enabled but also had to use long press in the same place!

I made a custom class that works.

public class TextViewLinkLongPressUrl extends TextView {

    private boolean isLongClick = false;

    public TextViewLinkLongPressUrl(Context context) {
        super(context);
    }

    public TextViewLinkLongPressUrl(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TextViewLinkLongPressUrl(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_UP && isLongClick) {
            isLongClick = false;
            return false;
        }

        if (event.getAction() == MotionEvent.ACTION_UP) {
            isLongClick = false;
        }

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            isLongClick = false;
        }

        return super.onTouchEvent(event);
    }

    @Override
    public boolean performLongClick() {
        isLongClick = true;
        return super.performLongClick();
    }
}

Solution 8 - Android

This worked for me for cardView and will work the same for listview inside adapter calss, within onBindViewHolder() function

holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return false;
            }
        });

Solution 9 - Android

If you want to do it in the adapter, you can simply do this:

itemView.setOnLongClickListener(new View.OnLongClickListener()
        {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(mContext, "Long pressed on item", Toast.LENGTH_SHORT).show();
            }
        });

Solution 10 - Android

    listView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        return false;
    }
});

Definitely does the trick.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontechayuView Question on Stackoverflow
Solution 1 - AndroidDinesh SharmaView Answer on Stackoverflow
Solution 2 - AndroidJonathan LinView Answer on Stackoverflow
Solution 3 - AndroidArdiView Answer on Stackoverflow
Solution 4 - AndroidnandeeshView Answer on Stackoverflow
Solution 5 - AndroidVijay SharmaView Answer on Stackoverflow
Solution 6 - AndroidAyyoubView Answer on Stackoverflow
Solution 7 - AndroidOliver DixonView Answer on Stackoverflow
Solution 8 - AndroidniksView Answer on Stackoverflow
Solution 9 - Androiduser846316View Answer on Stackoverflow
Solution 10 - AndroidKalaiyo5View Answer on Stackoverflow