7

I'm using ActionBarSherlock-4.1.0-0 and I would like to open my submenu in the Actionbar with the hardware menu button. I'm planing an update and in my old version I used the "normal" menu. I'd like to help users to get used to the new design.

I got the 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 listener to the hardware 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 couldn't find a method or anything else to call.

halfer
  • 19,824
  • 17
  • 99
  • 186
Paul Spiesberger
  • 5,630
  • 1
  • 43
  • 53

3 Answers3

11

I try this solution from Frederik with android actionbar and I run into the problem that the submenu opens and closes immediately. Changing to onKeyUp solved this issue.

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 do check if optionsMenu != null && optionsMenu.findItem(R.id.sub_menu) != null because of compatibility issues with older Android versions without actionbar. This isn't nessesary if you use ActionBarSherlock for all versions.

corban
  • 618
  • 6
  • 16
4

This is how I solved the issue

mainMenu.performIdentifierAction(id_of_menu_item, 0);

So in your case I would imagine it would be like this

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:

  • Store the menu in a local variable
  • Use that variable to look for the sub menu
  • Use that variable to call the performIdentifierAction method

Hopefully this will work.

Fredrik Sundmyhr
  • 801
  • 6
  • 16
  • Thanks for your support, but when I implement your solution, then I get a java.lang.NullPointerException at line mainMenu.performIdentifierAction(subMenu.getItem().getItemId(), 0);. The subMenu object is null, any advice? – Paul Spiesberger Jul 30 '12 at 17:47
  • The idea is that you need to find the item id of your submenu and use that. What is the id of you sub menu id? (The r.id.[theid])? What I do in my implementation is to store the MenuItem in in a local var and use that to do: getItemId(). – Fredrik Sundmyhr Jul 30 '12 at 18:59
4

I always got a NullPointerException with the solution of Fredrik Sundmyhr, then I changed a few things and it worked. Here's 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);
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Paul Spiesberger
  • 5,630
  • 1
  • 43
  • 53