0

I have a ListView filled with a CustomAdapter that should display a ContextMenu after a long click.

I try to do it like such:

// in OnCreateView
ListView list = (ListView) view.findViewById(R.id.list);

registerForContextMenu(list);

and

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.todo_context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.edit:
            // Do something
            return true;
        case R.id.delete:
            // Do something
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

And my todo_context_layout.xml being:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/edit" android:title="Edit"/>
    <item android:id="@+id/delete" android:title="Delete"/>
</menu>

A long click does trigger the OnItemLongClickListener but does not display a context menu. What am I doing wrong?

After reading the following thread, I could not find the answer to my question.

Community
  • 1
  • 1

1 Answers1

0

If you want to show a context menu you don't need a longclicklistener. Try without this listener.

beeb
  • 1,615
  • 1
  • 12
  • 24
  • Sorry didn't see.. Do you have an additional longclicklistener? – beeb Jan 09 '17 at 15:10
  • I have a `OnItemLongClickListener` yes, and it does get called, but not the `onCreateContextMenu` for some reason –  Jan 09 '17 at 15:11
  • If you want to show a context menu you don't need a longclicklistener. Try without this listener. – beeb Jan 09 '17 at 15:12
  • Thank you ! Didn't see your comment, please post it as an answer so I can accept it –  Jan 09 '17 at 15:16