0

As Android doesn't support icons in context menus, I decided to replace the native ActionBar More menu item with my own custom item. The problem is that it doesn't listen for the Menu hardware key presses. Is there any way I can implement this behavior? Actually the problem is to find a way to activate an item in ActionBar by pressing a key. Thanks in advance.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • I think your answer can be found [here](http://stackoverflow.com/questions/12277262/opening-submenu-in-action-bar-on-hardware-menu-button-click). – Tas Morf Dec 05 '12 at 11:55
  • @TasMorf, Nope, it doesn't answer my question. – Egor Dec 05 '12 at 11:58

1 Answers1

2

To display your menu (which is an ActionBar item as well) on a hardware menu button click, you can do this (you need to keep a reference to your Menu in the OnCreateOptionsMenu callback):

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode) {
        case KeyEvent.KEYCODE_MENU:
            mainMenu.performIdentifierAction(R.id.your_custom_menu, 0);
           return true;  
    }
    return false;
}
Tas Morf
  • 3,065
  • 1
  • 12
  • 8