21

I am trying to show the Option menu on button click.How can I do this can anyone tell me.

Kaushik
  • 6,150
  • 5
  • 39
  • 54
Altaf
  • 5,150
  • 10
  • 39
  • 55

1 Answers1

64

You can use openOptionsMenu to programmatically open the options menu.

If you have a Button, you can do:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        openOptionsMenu();
    }
});

or else set android:onClick="myOnClickMethod" on your Button in xml and then have:

public myOnClickMethod(View v) {
    openOptionsMenu();
}

in your activity.

Matthew
  • 44,826
  • 10
  • 98
  • 87
  • 7
    Warning -- this is completely insufficient in terms of cross-version and configuration requirements. It is not this simple once you start considering Honeycomb, ActionBars, ICS, 4.3 bugs, etc. – Cory Trese Aug 07 '13 at 07:06
  • 1
    @CoryTrese please tell some senarios where this can fail. If possible, giving solution to those problems will be remarkably thankful. – Rahul Rastogi Jun 04 '14 at 11:33
  • 1
    My project is completely on single activity with all fragments supporting version 2.3 onwards, not using android provided action bar, made my own layout header and using it as a action bar. – Rahul Rastogi Jun 04 '14 at 11:35
  • what happens with the activity when openOptionsMenu() is called? does it stop? does it pause? thx – iversoncru Feb 19 '15 at 08:18
  • 1
    I can say this much: openOptionsMenu() is failing on a Samsung Tab S 10.5 LTE running 5.0.2. It works on my other devices. – Edward Falk Aug 08 '15 at 23:33
  • @RahulRastogi Like you said above i am using layout header as action bar. How to show the overflow menu on button click. Please look into this SO question http://stackoverflow.com/questions/33297034/show-overflow-menu-on-image-button-click#33297127 and help me out. Thanks – Royal Oct 23 '15 at 10:13