Android Open Menu from a button

txagdev picture txagdev · Sep 15, 2010 · Viewed 14.9k times · Source

Is there a way for a button to call a submenu?

I currently have a menu where if they select one of the options it opens a submenu, however I want to be able to start the submenu from the Button widget instead of the Menu object.

Thanks!

Answer

Mark D picture Mark D · Sep 26, 2011

I am not necessarily recommending this, but I was able to call the submenu programatically. First, in the override of onCreateOptionsMenu I stored the menu parameter to a private Menu variable in the activity. Then, I was able to simply do the following from the onClick():

MyActivity.this.openOptionsMenu(); // activity's onCreateOptionsMenu gets called
optionsMenu.performIdentifierAction(R.id.miSubMenu1, 0);

In a particular instance this saved me a lot of time since the submenu was built months before the request to have an additional button go directly to the submenu.