How to open the options menu programmatically?

AndroidButtonMenuAndroid Activity

Android Problem Overview


I would like to open the optionsMenu programmatically without a click on the menu key from the user. How would I do that?

Android Solutions


Solution 1 - Android

Solution 2 - Android

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.

Solution 3 - Android

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();

Solution 4 - Android

Put this line of code in your onResume(), this should help!

new Handler().postDelayed(new Runnable() { 
   public void run() { 
     openOptionsMenu(); 
   } 
}, 1000); 

Solution 5 - Android

from an OnClickListener inside an activity called MainActivity:

MainActivity.this.openOptionsMenu();

Solution 6 - Android

if using AppCompatActivity

getSupportActionBar().openOptionsMenu();

Solution 7 - Android

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);

Solution 8 - Android

If you are inside an your view, you can write

    ((Activity)getContext()).openOptionsMenu();

Solution 9 - Android

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);

Solution 10 - Android

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);

Solution 11 - Android

toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitleTextColor(0xFFFFFFFF);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            toolbar.showOverflowMenu();
        }
    }, 100);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlaBRAView Question on Stackoverflow
Solution 1 - AndroidRobby PondView Answer on Stackoverflow
Solution 2 - AndroidKovačView Answer on Stackoverflow
Solution 3 - AndroidmarmorView Answer on Stackoverflow
Solution 4 - AndroidRoyston PintoView Answer on Stackoverflow
Solution 5 - AndroidOded BreinerView Answer on Stackoverflow
Solution 6 - AndroidAbhijitView Answer on Stackoverflow
Solution 7 - AndroidCristianView Answer on Stackoverflow
Solution 8 - AndroidFabio BianconiView Answer on Stackoverflow
Solution 9 - AndroidLotfiView Answer on Stackoverflow
Solution 10 - AndroidNanda GopalView Answer on Stackoverflow
Solution 11 - AndroidMukesh KrishMegView Answer on Stackoverflow