Android Open Menu from a button

岁酱吖の 提交于 2019-12-04 05:55:54

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.

You can use the openOptionsMenu() on the activity to programmatically open the activity from the button, but you can't do the same for a submenu.

I'm not sure if thats possible. One option would be to use a dialog as the sub menu, so that you can open it from either location.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!