Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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?

like image 588
rabar kareem Avatar asked Oct 19 '25 14:10

rabar kareem


1 Answers

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);
}
like image 117
Karakuri Avatar answered Oct 22 '25 03:10

Karakuri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!