57

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

Janusz
  • 187,060
  • 113
  • 301
  • 369
BlaBRA
  • 2,201
  • 4
  • 18
  • 13

11 Answers11

94

Or just call Activity.openOptionsMenu()?

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
33

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.

Kovač
  • 331
  • 3
  • 2
16

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();
marmor
  • 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
11

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

new Handler().postDelayed(new Runnable() { 
   public void run() { 
     openOptionsMenu(); 
   } 
}, 1000); 
Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
9

from an OnClickListener inside an activity called MainActivity:

MainActivity.this.openOptionsMenu();
Oded Breiner
  • 28,523
  • 10
  • 105
  • 71
4

if using AppCompatActivity

getSupportActionBar().openOptionsMenu();
Abhijit
  • 360
  • 4
  • 14
1

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);
Cristian
  • 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
0

If you are inside an your view, you can write

    ((Activity)getContext()).openOptionsMenu();
-1

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);
Syden
  • 8,425
  • 5
  • 26
  • 45
Lotfi
  • 660
  • 1
  • 6
  • 21
-1

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);
Nanda Gopal
  • 2,519
  • 1
  • 17
  • 25
-1
toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitleTextColor(0xFFFFFFFF);

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