ActionBarSherlock: open a submenu using the menu button

I am using ActionBarSherlock-4.1.0-0, and I would like to open my submenu in the action bar using the hardware menu button. I am planning an upgrade, and in my old version I used the "normal" menu. I would like to help users get used to the new design. I got a submenu and the main menu:

@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater i = getSupportMenuInflater(); i.inflate(R.menu.main_menu, menu); SubMenu subMenu = (SubMenu) menu.findItem(R.id.actionbar_submenu); Menu mainMenu = menu; return super.onCreateOptionsMenu(menu); } 

and I got a list for the equipment menu button:

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(event.getAction() == KeyEvent.ACTION_DOWN){ switch(keyCode) { case KeyEvent.KEYCODE_MENU: // TODO: expand submenu from actionbar return true; } } return super.onKeyDown(keyCode, event); } 

I could not find a way or anything else to call. I hope you can help me, amuses, Paul

+7
source share
3 answers

I try this solution from Frederik using the android action bar, and I ran into a problem that the submenu opens and closes immediately. Switching to onKeyUp solved this problem.

Here is my code:

 @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_MENU){ if (event.getAction() == KeyEvent.ACTION_DOWN && optionsMenu != null && optionsMenu.findItem(R.id.sub_menu) != null) { Log.i(TAG, "performIdentifierAction"); optionsMenu.performIdentifierAction(R.id.sub_menu, 0); return true; } } return super.onKeyUp(keyCode, event); } 

I check if there are optionsMenu != null && optionsMenu.findItem(R.id.sub_menu) != null due to compatibility issues with older versions of Android without an action bar. This is not necessary if you use ActionBarSherlock for all versions.

+11
source

This is how I solved the problem

 mainMenu.performIdentifierAction(id_of_menu_item, 0); 

So, in your case, I would suggest that it would be so.

 private Menu mainMenu; // local variable for menu @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater i = getSupportMenuInflater(); i.inflate(R.menu.main_menu, menu); SubMenu subMenu = (SubMenu) menu.findItem(R.id.actionbar_submenu); mainMenu = menu; // store the menu in an local variable return super.onCreateOptionsMenu(menu); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(event.getAction() == KeyEvent.ACTION_DOWN){ switch(keyCode) { case KeyEvent.KEYCODE_MENU: SubMenu subMenu = (SubMenu) mainMenu.findItem(R.id.actionbar_submenu); mainMenu.performIdentifierAction(subMenu.getItem().getItemId(), 0); return true; } } return super.onKeyDown(keyCode, event); } 

In short:

  • Saving a menu in a local variable
  • Use this variable to search for submenus.
  • Use this variable to call the performIdentifierAction method

Hope this works.

+4
source

I always got a NullPointerException using the Fredrik Sundmyhr solution, then I changed a few things and it worked. Here is my solution:

 @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if(event.getAction() == KeyEvent.ACTION_UP){ switch(keyCode) { case KeyEvent.KEYCODE_MENU: SubMenu subMenu = mainMenu.getItem(2).getSubMenu(); mainMenu.performIdentifierAction(subMenu.getItem().getItemId(), 0); return true; } } return super.onKeyUp(keyCode, event); } 

amuses Paul

+4
source

All Articles