Show context menu from code behind Show context menu from code behind android android

Show context menu from code behind


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.


 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);


you can use any of the following:

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

  2. showContextMenuForChild