Android Option Menu on Button click
Asked Answered
T

1

21

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

Tab answered 4/4, 2011 at 1:29 Comment(0)
H
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.

Horst answered 4/4, 2011 at 1:33 Comment(6)
Warning -- this is completely insufficient in terms of cross-version and <Application /> configuration requirements. It is not this simple once you start considering Honeycomb, ActionBars, ICS, 4.3 bugs, etc.Peritonitis
@CoryTrese please tell some senarios where this can fail. If possible, giving solution to those problems will be remarkably thankful.Fulmer
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.Fulmer
what happens with the activity when openOptionsMenu() is called? does it stop? does it pause? thxSublease
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.Alcoran
@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 #33297534 and help me out. ThanksMelancholic

© 2022 - 2024 — McMap. All rights reserved.