javaandroidandroid-layoutandroid-contextmenu

onCreateContextMenu is not being called


I am using the following code to generate onCreateContextMenu, however, I am not getting any response when clicking on a list item.

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenu.ContextMenuInfo menuInfo) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    int currentId = (int) info.id;
    menu.add(0, MENU_DELETE_ID, 0, "Delete");
}

I will be using currentId later on, but the above code does not results in a popup with the word Delete in it.

Could it because I am using a custom AdapterView which is shown in this answer to my previous question? Also, my MainActivity is extending AppCompatActivity if that matters.

I've checked other questions such as this one onCreateContextMenu isn't being called but I am not using onItemLongClickListener


Solution

  • There's not enough code to understand what's actually wrong here. But I might suggest some frequent mistake to be taken care of while implementing the ContextMenu.

    You need to register the context menu first. From the developers documentation of creating a context menu -

    If your activity uses a ListView or GridView and you want each item to provide the same context menu, register all items for a context menu by passing the ListView or GridView to registerForContextMenu().

    So you might consider doing something like this in your onCreate function of your ListActivity.

    registerForContextMenu(getListView());
    

    And I don't see any MenuInflater in your onCreateContextMenu. You need to inflate the view while creating the context menu.

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

    From the documentation

    MenuInflater allows you to inflate the context menu from a menu resource. The callback method parameters include the View that the user selected and a ContextMenu.ContextMenuInfo object that provides additional information about the item selected. If your activity has several views that each provide a different context menu, you might use these parameters to determine which context menu to inflate.

    And you might have to implement a long click listener for your list items. As it seems to work with long click events only.

    When the registered view receives a long-click event, the system calls your onCreateContextMenu() method. This is where you define the menu items, usually by inflating a menu resource.

    Here you go for the full implementation documentation. Hope that helps!

    Update

    If you're not using ListActivity you should not be able to call getListView. In that case, just pass the ListView reference while registering your context menu for your list.

    ListView lv = (ListView) findViewById(R.id.my_list);
    registerForContextMenu(lv);