Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Registering ContextMenu for a Custom ListAdapter

I made a custom list adapter extending the Base adapter. Each item in the list has an imagebutton, 2 textviews and a button. I tried to add the context menu to the list so as to display some options for an item in the list.

registerForContextMenu(getListView());

I used a MenuInflater object to inflate the context menu xml file. But on clicking the items in the list nothing shows up or the usual highlighting of the item of list on click isn't shown. Is it that the context menu doesn't work for custom list views? Any help would be much appreciated.

Regards, Primal

like image 450
Primal Pappachan Avatar asked Jan 19 '26 15:01

Primal Pappachan


2 Answers

Make sure child items of ListView must be Long Clickeable.

It can be done in the xml layout file, for example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:longClickable="true">

    <!-- Child elements -->

</LinearLayout>

Or it can be done in java code:

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
   CustomView customView = new CustomView();
   customView.setLongClickeable(true);
}

I hope it helps.

like image 98
David Salgado Avatar answered Jan 23 '26 01:01

David Salgado


It should work for custom list adapters. The only thing that comes to mind is that registerForContextMenu() needs to be called after setListAdapter() (if you're using ListActivity).

like image 33
Erich Douglass Avatar answered Jan 22 '26 23:01

Erich Douglass