rabar kareem
rabar kareem

Reputation: 630

How to disable menu button and the action bar still remains there

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?

Upvotes: 4

Views: 2986

Answers (1)

Karakuri
Karakuri

Reputation: 38605

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

Upvotes: 7

Related Questions