18

This might be a simple question, but I've been looking around and can't find the answer. Is there any code to show the context menu on Android from a code, instead of pressing the menu button? E.g. when I touch the screen then it'll call the context menu?

user12205
  • 2,684
  • 1
  • 20
  • 40
AnD
  • 3,060
  • 8
  • 35
  • 63

4 Answers4

39

Call openContextMenu() on your Activity whenever you want to open it. Note that this is a rather unusual UI pattern, one that your users may not expect.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
7
 OnClickListener onClick_Show_Contextmenu = new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) context).openContextMenu(v);
            }

        };

        findViewById(R.id.xxx).setOnClickListener(onClick_Show_Contextmenu);

        registerForContextMenu(findViewById(R.id.xxx));
        findViewById(R.id.xxx).setLongClickable(false);
hector6872
  • 1,336
  • 14
  • 18
4

you can use any of the following:

  1. openContextMenu as shown here:
registerForContextMenu(view); 
openContextMenu(view);
unregisterForContextMenu(view);
  1. setOnCreateContextMenuListener

  2. showContextMenuForChild

android developer
  • 114,585
  • 152
  • 739
  • 1,270
1

You can use

view.showContextMenu();

on your view.

B-GangsteR
  • 2,534
  • 22
  • 34