Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show context menu from code behind

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?

like image 631
AnD Avatar asked Sep 12 '10 17:09

AnD


2 Answers

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.

like image 139
CommonsWare Avatar answered Oct 21 '22 17:10

CommonsWare


 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);
like image 36
hrules6872 Avatar answered Oct 21 '22 16:10

hrules6872