4

I have a problem in disabling menu button , I dont want the menu button to be enable , I disable is by returning false in onPrepareOptionsMenu function , but it hides all action items in my action bar, so How to disable menu button without affecting my actionbar?

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
rabar kareem
  • 630
  • 9
  • 28
  • What menu button are you taking about? Is it a hardware menu button? Is it the overflow (three vertical dots) button on the ActionBar? Or is it the overflow icon that appears on the navigation bar next to the Recents button? – Karakuri Mar 31 '13 at 16:48
  • Oh I forgot to mention , I mean a hardware one – rabar kareem Mar 31 '13 at 16:51

1 Answers1

7

Hardware menu button is not controlled by onPrepareOptionsMenu(). Generally speaking, it is not good practice to change the behavior of hardware buttons because users expect it to behave a certain way (which I believe is to expand the overflow menu).

If you absolutely have to disable it, you could listen for it to be pressed in our Activities like this:

public boolean dispatchKeyEvent(KeyEvent event) {
    final int keycode = event.getKeyCode();
    final int action = event.getAction();
    if (keycode == KeyEvent.KEYCODE_MENU && action == KeyEvent.ACTION_UP) {
        return true; // consume the key press
    }
    return super.dispatchKeyEvent(event);
}
Karakuri
  • 38,365
  • 12
  • 84
  • 104