Opening submenu in action bar on Hardware menu button click
Asked Answered
C

1

18

Title explains everything. I want to open a submenu in actionbar when clicking Hardware menu button

This is the code and it works fine first time i click menu. Every other time it just flashes(opens and the instantly closes it)

private Menu mainMenu;
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
mainMenu = menu;

return true;
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
    switch(keyCode) {
    case KeyEvent.KEYCODE_MENU:

        mainMenu.performIdentifierAction(R.id.more, 0);

        return true;  
    }
}
return super.onKeyDown(keyCode, event);
}

and here is options.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/settings"
    android:icon="@drawable/ic_menu_preferences"
    android:showAsAction="ifRoom|withText"
    android:title="Settings"/>

<item
    android:id="@+id/about"
    android:icon="@drawable/ic_menu_info_details"
    android:showAsAction="ifRoom|withText"
    android:title="About"/>

<item
    android:id="@+id/more"
    android:icon="@drawable/ic_menu_moreoverflow_normal_holo_dark"
    android:showAsAction="always|withText"
    android:title="More">
    <menu>
        <item
    android:id="@+id/changelog"
    android:icon="@drawable/ic_menu_recent_history"
    android:showAsAction="ifRoom|withText"
    android:title="Changelog"/>
        <item
    android:id="@+id/update"
    android:icon="@drawable/ic_menu_refresh"
    android:showAsAction="ifRoom|withText"
    android:title="Update Check"/>
<item
    android:id="@+id/check"
    android:icon="@drawable/ic_menu_help"
    android:showAsAction="ifRoom|withText"
    android:title="Compatibility Check"/>
        </menu> 
        </item>

</menu>

UPDATE:(Solution) Just changed onKeyDown() method to onKeyUp() and now it sticks

Centriole answered 5/9, 2012 at 8:21 Comment(2)
Thank you so much for your question! I have been looking for a solution!Arsenite
Thanks for update for solutionMilinda
C
6

Try this:

public boolean onCreateOptionsMenu(Menu menu) 
{
    super.onCreateOptionsMenu(menu);// <--- add this

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options, menu);
    mainMenu = menu;
    return true;
}

//override this method instead of onKeyDown()....
@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    super.onOptionsItemSelected(item);      

    int menuId = item.getItemId();      
    if(menuId == R.id.settings)
    {
        //do settings   
    }
    //else if(menuId = ...) {....}

    return true;
}
Caveman answered 5/9, 2012 at 8:40 Comment(12)
And how would that get called when i click menu?Centriole
I already have onOptionsItemSelected for every item, but what i want to do is when i click hardware menu key to open submenuCentriole
It is what onOptionsItemSelected(MenuItem item) do for all the menu Items. Inside this method, you can check which menu item is selected base on menuId (use if..else.. or switch..case..)Caveman
Yes, but only if you click it manualy. I want to open it when clicking menu key on devicesCentriole
Menu is always opened if you implement your onCreateOptionsMenu(Menu menu) inside your current activity. No code effort to open it. I don't catch your idea in "but only if you click it manualy".Caveman
All of my Menu items are in ActionBar, i dont have to press menu to show them. But as you can see in options.xml i have a submenu which i want to open when clicking menu keyCentriole
Are you trying to execute one menu item when press device's menu button? It will trick you because: it open the menu for select (by android default), then immediately closed to perform select action. Why you have to do so. Please remember that you are implementing Context Menu for your activity.Caveman
Yes exactly what im trying to do. Let me give you a screenshotCentriole
You Comment out (or remove) all your public boolean onKeyDown(int keyCode, KeyEvent event) to see what happend. Don't try to open submenu by handling click.Caveman
Why not to open submenu by handling menu click? How else to open it?Centriole
let us continue this discussion in chatCentriole
Android opens the parent menu attached to the activity while press menu key by default. Sub menu is opened when user selects parent menu item. If you try to open submenu in the same time, I don't think its the way Android need to have, it breaks the menu logic.Caveman

© 2022 - 2024 — McMap. All rights reserved.