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

pedja
  • 3,285
  • 5
  • 36
  • 48

1 Answers1

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;
}
Kelvin Trinh
  • 1,248
  • 7
  • 15
  • And how would that get called when i click menu? – pedja Sep 05 '12 at 08:42
  • I already have onOptionsItemSelected for every item, but what i want to do is when i click hardware menu key to open submenu – pedja Sep 05 '12 at 08:44
  • 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..) – Kelvin Trinh Sep 05 '12 at 08:45
  • Yes, but only if you click it manualy. I want to open it when clicking menu key on devices – pedja Sep 05 '12 at 08:47
  • 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". – Kelvin Trinh Sep 05 '12 at 08:49
  • 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 key – pedja Sep 05 '12 at 08:51
  • 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. – Kelvin Trinh Sep 05 '12 at 08:53
  • Yes exactly what im trying to do. Let me give you a screenshot – pedja Sep 05 '12 at 08:56
  • 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. – Kelvin Trinh Sep 05 '12 at 09:01
  • Why not to open submenu by handling menu click? How else to open it? – pedja Sep 05 '12 at 09:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16283/discussion-between-user1202206-and-kelvin-trinh) – pedja Sep 05 '12 at 09:10
  • 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. – Kelvin Trinh Sep 05 '12 at 09:12