Show custom action item menu on Menu button click

1.6k Views Asked by At

I am trying to create a custom menu button as an Action item, here is the xml resource I used

<menu 
    xmlns:spyder="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/menu_search_customers"
      android:title="@string/hello_world"
      android:icon="@android:drawable/ic_menu_search"
      spyder:showAsAction="ifRoom|collapseActionView"
      spyder:actionViewClass="android.support.v7.widget.SearchView" />

    <item
        android:id="@+id/menu_overflow"
        android:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_light"
        android:orderInCategory="11111"
        spyder:showAsAction="always">
        <menu>
            <item
                android:id="@+id/menu_overflow_item1"
                spyder:showAsAction="never"
                android:title="Item1"/>
            <item
                android:id="@+id/menu_overflow_item2"
               spyder:showAsAction="never"
                android:title="Item2"/>
        </menu>
</item>
</menu>

And here is the output

enter image description here

Menu popups when i click on action item as expected, I want to popup it on Hardware Menu button click also. is that possible?

1

There are 1 best solutions below

1
On

You can capture the hardware menu click using the following function.

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        openOptionsMenu()
        return true;
    }
    return super.onKeyDown(keyCode, event);
}