mhmtemnacr
mhmtemnacr

Reputation: 305

Display menu item in both action bar and menu key

I have a menu item in res/menu/mymenu.xml:

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

    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/ic_action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        android:showAsAction="ifRoom"  />

</menu>

But when I press menu key the menu doesn't appear. It only shows in action bar. Also I tried android:showAsAction="never" but that time the item only shows in the menu key screen. I want to display the item both in action bar, and in the screen that shows when I press menu key. Thanks in advance.

Upvotes: 0

Views: 1835

Answers (2)

Irene Maryanne
Irene Maryanne

Reputation: 21

Just duplicate the menu item in your_menu.xml with the same title but different id. Then in onOptionsItemSelected() use the same code function as where R.id.action_settingsShown lead to for the new R.id.action_settingsHidden.

<item android:id="@+id/action_settingsShown"
    android:title="@string/settings"
    android:icon="@drawable/ic_action_settings_dark"
    android:showAsAction="ifRoom"/>

<item android:id="@+id/action_settingsHidden"
    android:title="@string/settings"
    android:icon="@drawable/ic_action_settings_dark"
    android:showAsAction="never"/>

Upvotes: 0

Paul Lammertsma
Paul Lammertsma

Reputation: 38252

Bear in mind that all menu items are displayed only once. You cannot instruct the menu to display a single menu item twice, whether that's once in the overflow, and once in the action bar.

With android:showAsAction="never", you're instructing Android to tuck the menu item away into the overflow menu. (On devices without a hardware menu button, the menu item will display in the action bar overflow. On devices with a hardware menu button, the menu item will display in a pop-up menu from the bottom of the screen, which is also known as the action bar overflow.)

With android:showAsAction="always", you do the opposite; it will always appear in the action bar, whether or not there's enough room.

Although I thoroughly discourage your from taking this approach, strictly answering your question is to provide two menu items: one with mode never, the other with mode always.

I would instead suggest you reconsider whether you want the item in the overflow menu or not. Consider carefully Google's recommendations on the action bar, and either display it or place it in the overflow--not both. Also bear in mind that different device configurations may allow for more items displayed if there is enough room when using ifRoom.

Upvotes: 5

Related Questions