how to implement a long click listener on a listview
Asked Answered
R

10

160

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;
            }
}); 
Rangefinder answered 13/1, 2012 at 6:19 Comment(3)
Have you remembered to add "implements OnItemLongClickListener" to your class declaration?Forfeit
see in xml whether long click is enable?Dynamo
Maybe you have a Gesture Listener or something like this that is capturing the long press and consuming it.Nynorsk
T
335

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.

Textualist answered 13/1, 2012 at 6:36 Comment(5)
Be sure to call lv.setLongClickable(true); also.Constrict
This didn't work for me. But this does: lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {...Splenius
somehow adroid:longClickable="true" is default. I'm using API 19. So I didn't need to specify it at all.Sikes
Android sets longClickable=true when setting the listener.Paganism
what value is stored in long id ? in public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long idBrabazon
O
28

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.

Obovoid answered 29/5, 2013 at 9:2 Comment(1)
that's true...but how about the background selected color, do we need to write the code too ... @Jonathan ?Introduction
S
15

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;
            }
}); 
Salenasalene answered 7/1, 2013 at 12:57 Comment(0)
P
6

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.

Pilferage answered 13/1, 2012 at 6:24 Comment(0)
C
2

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;
            }
        });
Canonry answered 15/4, 2018 at 5:46 Comment(0)
S
1

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

Serrated answered 30/11, 2016 at 11:45 Comment(0)
S
1

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();
    }
}
Sherr answered 1/12, 2016 at 13:23 Comment(0)
C
0

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;
            }
        });
Carlotacarlotta answered 19/10, 2018 at 17:41 Comment(0)
K
0

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();
            }
        });
Kohlrabi answered 3/3, 2019 at 20:4 Comment(0)
B
0
    listView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        return false;
    }
});

Definitely does the trick.

Budgerigar answered 2/9, 2019 at 3:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.