I would like to open the optionsMenu programmatically without a click on the menu key from the user. How would I do that?
11 Answers
Or just call Activity.openOptionsMenu()?

- 73,164
- 16
- 126
- 119
-
1Is is possible to execute this statement in oncreate() method of Activity? – Vikas Apr 12 '11 at 04:57
-
@Vikas. Put it in onAttachedToWindow(), as described by Kovac below. – Stephen Hosking Nov 01 '12 at 12:03
-
2not working, I had custom view in actionbar, and on childview onClick I am calling open/close optionMenu, also with super.open/close not working, any idea, where I am mistaking, – Abdul Wahab May 18 '14 at 10:05
-
1How to implement this in a Fragment?? – Kartiikeya Dec 15 '16 at 12:31
Apparently, doing it in onCreate breaks app, since Activity's not yet attached to a window. If you do it like so:
@Override
public void onAttachedToWindow() {
openOptionsMenu();
};
...it works.

- 331
- 3
- 2
-
9Yes, it works. However, it would be better to have `super.onAttachedToWindow()` as the first line of the function. – Stephen Hosking Nov 01 '12 at 21:54
For developers using the new Toolbar
class of the Support Library
, this is how it's done:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.showOverflowMenu();

- 27,641
- 11
- 107
- 150
-
im doing this in an activity with a collapsing toolbar as well as a toolbar and it doesnt work4me no matter where I put it, plz help – Manny265 Sep 18 '16 at 23:25
Put this line of code in your onResume(), this should help!
new Handler().postDelayed(new Runnable() {
public void run() {
openOptionsMenu();
}
}, 1000);

- 6,681
- 2
- 28
- 46
from an OnClickListener inside an activity called MainActivity:
MainActivity.this.openOptionsMenu();

- 28,523
- 10
- 105
- 71
Two ways to do it:
Activity.getWindow().openPanel(Window.FEATURE_OPTIONS_PANEL, event);
The event
argument is a KeyEvent
describing the key used to open the menu, which can modify how the menu is displayed based on the type of keyboard it came from.
Or... by simulating that the user has pressed the button:
IWindowManager wManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SOFT_LEFT);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_SOFT_LEFT);
wManager.injectKeyEvent(kd.isDown(), kd.getKeyCode(), kd.getRepeatCount(), kd.getDownTime(), kd.getEventTime(), true);

- 198,401
- 62
- 356
- 264
-
I think your answer is the best because openOptionsMenu() withh cause application crashing on HTC one and Sony Xperia Acro , while your solution avoids this point...So Thank yOu – Muhannad A.Alhariri Sep 24 '13 at 08:34
-
IWindowManager is not a public part of the SDK. You did you get a reference to it? – Andrew S Jun 23 '15 at 01:42
If you are inside an your view, you can write
((Activity)getContext()).openOptionsMenu();

- 11
- 2
After a long research and many tries, the only way seems to be simulating a KeyEvent
. This makes the options menu appear:
BaseInputConnection mInputConnection = new BaseInputConnection( findViewById(R.id.main_content), true);
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU);
mInputConnection.sendKeyEvent(kd);
mInputConnection.sendKeyEvent(ku);
For me, declaring toolbar.showOverflowMenu()
in onClick is not worked. openOptionsMenu()
also not worked for me. Instead of that the following way is worked for me,
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
toolbar.showOverflowMenu();
}
}, 500);

- 2,519
- 1
- 17
- 25
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(0xFFFFFFFF);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
toolbar.showOverflowMenu();
}
}, 100);

- 1
- 1