Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Open Menu from a button

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!

like image 399
txagdev Avatar asked Sep 15 '10 18:09

txagdev


People also ask

How do I open the pop up menu on android?

Go to app > res > right-click > New > Android Resource Directory and give Directory name and Resource type as menu. Now, we will create a popup_menu file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu.

What is contextual menu in android?

A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

What is onCreateOptionsMenu in android?

If you've developed for Android 3.0 and higher, the system calls onCreateOptionsMenu() when starting the activity, in order to show items to the app bar.

What is Android system menu?

The Android System Settings menu allows you to control most aspects of your device—everything from establishing a new Wi-Fi or Bluetooth connection, to installing a third-party onscreen keyboard, to adjusting system sounds and screen brightness.


2 Answers

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.

like image 84
Mark D Avatar answered Nov 01 '22 10:11

Mark D


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.

like image 24
Harsh Kalra Avatar answered Nov 01 '22 10:11

Harsh Kalra