Android Dialogs and onContextItemSelected()

ListViews and GridViews are great widgets to use in an Activity for your app. Sometimes it is useful to also create smaller versions of them for dialogs within your app. There’s only one problem: Context Menus. If you have tried to put a context menu on a ListView inside a dialog, you may have found out that while onCreateContextMenu() is called, onContextItemSelected() is not. There is a workaround, though, because onContextItemSelected() is actually a convenience method called from onMenuItemSelected(int, MenuItem). If you add the following method to your dialog containing the ListView (or GridView), you will find that onContextItemSelected() is working again.

public boolean onMenuItemSelected(int aFeatureId, MenuItem aMenuItem) {
    if (aFeatureId==Window.FEATURE_CONTEXT_MENU)
        return onContextItemSelected(aMenuItem);
        return super.onMenuItemSelected(aFeatureId, aMenuItem);


2 thoughts on “Android Dialogs and onContextItemSelected()

  1. Hi
    Do you know what is needed to make onContextMenuClosed() be called?
    Im using the context menu (android 4.0) inside a Dialog class. In my case the trick with onMenuItemSelected was not need. I tried onOptionsMenuClosed but it is not called neither.

    Please reply to my email in case of some clues 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *