Long click on list activity item

20735 views
Skip to first unread message

Thao

unread,
Dec 1, 2008, 7:12:03 AM12/1/08
to Android Developers
Hi all,

I have an Activity that implements a ListActivty. In there, I have a
list of item that is wrapped by a custom adapter.

I would like to enable long click on each item in my list, but I can't
figure out how to do this...Could someone give an example please ?

Thank.

tranbi...@gmail.com

unread,
Jan 2, 2009, 4:04:20 AM1/2/09
to Android Developers
Hi,

Maybe this can help:

@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
Log.e(TAG, "bad menuInfo", e);
return;
}

long l = getListAdapter().getItemId(info.position);

an...@akapost.com

unread,
Feb 22, 2009, 2:53:24 PM2/22/09
to Android Developers
Try looking at:

AdapterView.onItemLongClick()

ListView implements AdapterView, so you'll have access to that juicy
method, which will pass the id of the item clicked as well as the
item's view the same as with onListItemClick().

{begin code}

// ListActivity has a ListView, which you can get with:
ListView lv = getListView();

// Then you can create a listener like so:
lv.setOnItemLongClickListener( new AdapterView.OnItemLongClickListener
(){
@Override
public boolean onItemLongClick(AdapterView<?> av, View v, int
pos, long id) {
onLongListItemClick(v,pos,id);
return false;
}
});

// You then create your handler method:
protected void onLongListItemClick(v,pos,id) {
Log.i( TAG, "onLongListItemClick id=" + id );
}

{end code}

The standard onListItemClick() will still fire as well; my best guess
there is to keep a suppression boolean around, which isn't pretty.
Any other thoughts on this?

Phrases I searched for before finally stumbling across the proper
docs:
listactivity hold
listactivity longclick
android list long click
listactivity long click
listactivity long touch
android listview onLongClickListener



On Jan 2, 4:04 am, "tranbinh.b...@gmail.com" <tranbinh.b...@gmail.com>
wrote:

matthias

unread,
Mar 31, 2009, 8:39:09 AM3/31/09
to Android Developers
Hi,

On Feb 22, 9:53 pm, an...@akapost.com wrote:
> ...
> The standard onListItemClick() will still fire as well; my best guess
> there is to keep a suppression boolean around, which isn't pretty.
> Any other thoughts on this?

just return true from onItemLongClick(); this means you have consumed
the event, so it won't propagate up to the normal click handler.

Cheers,
Matthias

Kaptkaos

unread,
May 18, 2012, 7:12:24 PM5/18/12
to android-d...@googlegroups.com
Ah, this was exactly what I needed. Thanks for the tip!
Reply all
Reply to author
Forward
0 new messages