2

I am writing an app in which I need to disable Menu button.Now on this button press it minimises and sends my app to background.

I want to get an Event triggered in my activity if someone has pressed the menu button.

I have tried following links on Stack Overflow

Detecting physical Menu key press in Android

Android: How to add listener to hardware menu button?

None of these worked for me.

Following is my Code

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){


    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
        Toast.makeText(getBaseContext(),"Please use the top navigation icons ...",Toast.LENGTH_SHORT).show();
    }else if(keyCode == KeyEvent.KEYCODE_MENU)
    {
        Toast.makeText(getBaseContext(),"Please use the top navigation icons ...",Toast.LENGTH_SHORT).show();
    }else
    {

        return super.onKeyDown(keyCode, event);
    }

    return true;
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        // ........
        return true;
    }else
    {
        return super.onKeyUp(keyCode, event);
    }
}

@Override
public boolean onPrepareOptionsMenu (Menu menu) {
    if (true)
        menu.getItem(1).setEnabled(false);
    return false;
}
Community
  • 1
  • 1
Tejas
  • 358
  • 5
  • 23
  • Why would your app be minimised when the menu button is pressed? Are you sure you don't mean the home button? – Kane O'Riley Apr 21 '16 at 16:14
  • Definitely I didn't mean Home button. When I press the menu button it says "No recently used apps" in black window. Instead of this I want to pop up a toast message – Tejas Apr 25 '16 at 05:17
  • 1
    That's not the menu button, it's the recent apps/multitasking button. And no, you can't override it. That button is locked down by the operating system. – Kane O'Riley Apr 25 '16 at 10:33
  • Check http://stackoverflow.com/questions/12039004/how-to-ignore-button-click-of-recent-activity-button-in-android-3-0 for a more detailed answer to your question. – Kane O'Riley Apr 25 '16 at 10:35
  • You are correct. we can't handle this key without rooting it. I have overridden the button by rooting the devices and altered the Generic.kl file in system as per requirement. Now I have full control. – Tejas Apr 26 '16 at 04:32

0 Answers0