0

In the below menu.xml, I am trying to display the menu_exit in the overflow menu icon, so as shown below I set its showAsAction="never", but when I run the app, the overflow menu does not show up.

My questions are:

  1. How to place the last item in the menu.xml menu_exit in the overflow menu?
  2. the 3rd item in the menu.xml, when it is disabled, shows up in the settings menu (the lower left button on the device), and what I want is, when it is disabled it should not appear any where, only on the ActionBar when it is enabled.

menu.xml:

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

<item
    android:id="@+id/menu_refresh"
    android:orderInCategory="1"
    android:showAsAction="ifRoom"/>
<item
    android:id="@+id/menu_scan"
    android:checkable="true"
    android:orderInCategory="2"
    android:title="@string/menu_scan"
    android:showAsAction="ifRoom|withText"/>
<item
    android:id="@+id/menu_stop"
    android:title="@string/menu_stop"
    android:orderInCategory="3"
    android:showAsAction="ifRoom|withText"/>
<item
    android:id="@+id/menu_exit"
    android:title="@string/menu_stop"
    android:orderInCategory="4"
    android:showAsAction="never"/>
</menu>
hata
  • 11,633
  • 6
  • 46
  • 69
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • to be clear, when overflow button doesn't show up -- the exit item isn't accessible at all? Does your device have a hardware menu button? – Ivan Bartsov Sep 03 '15 at 14:13
  • @IvanBartsov if u mean the lower left button on the device,then yes my device has it – Amrmsmb Sep 03 '15 at 14:14
  • oh, that's your issue. Overflow button doesn't show up on devices with hardware menu button, hang on, I'll dig up the workaround... – Ivan Bartsov Sep 03 '15 at 14:17

5 Answers5

3

Note: this answer is now very dated.

tnx @gMale for highlighting the following:

Caution: Using reflection to read this field will now throw an exception when targeting API 31 and above.

Original answer:

Prior to Android 4.4, if device has a hardware menu button, action overflow menu is activated with the hardware button and the overflow (aka three dot) icon is not shown.

Android 4.4+ devices will show action overflow icon even if hardware menu button is present (and also, Google tried to convince manufacturers to ditch the hardware menu button, as you see, vendors are pretty stubborn)

To force pre-4.4 devices to show action overflow button even if they have a hardware button, put this in your activity's onCreate()

try {
    ViewConfiguration config = ViewConfiguration.get(this);
    Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
    if (menuKeyField != null) {
        menuKeyField.setAccessible(true);
        menuKeyField.setBoolean(config, false);
    }
} catch (Exception ex) {
    // Ignore
}
Ivan Bartsov
  • 19,664
  • 7
  • 61
  • 59
  • I tried and despit my android version is 4.4.2 the code worked, but when i press the hardware menu button the response is as if i pressed the overflow icon.in other words, whther i pressed the overflow icon or the hardware menu button there will be a mneu appers from the overflow menu button – Amrmsmb Sep 03 '15 at 15:00
  • Yes, that's the way it will work, I don't know any way to display it differently for the two menu opening procedures. But that's ok, action overflow is the preferred UI pattern anyway so don't worry about that – Ivan Bartsov Sep 03 '15 at 15:02
  • can i disable the menu button? – Amrmsmb Sep 03 '15 at 15:03
  • Yes, you can (http://stackoverflow.com/a/15732036/375929), but you shouldn't. User may be used to using (gah) the hardware button, don't deprive them of that habit. The menu appearing next to action overflow button will also cause a slight dissonance and subtly suggest they should be using that button instead :) – Ivan Bartsov Sep 03 '15 at 15:05
  • 1
    Caution: Using reflection to read this field will now throw an exception when targeting API 31 and above. – gMale Feb 18 '22 at 19:12
0

Here's my XML file for a menu..

<item
    android:id="@+id/action_send_feedback"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="@string/action_send_feedback"/>

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

<item
    android:id="@+id/action_rate_app"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_rate_app"/>

Java code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

For android phones which have option button (at the bottom of the phone) the menu item which are showAsAction="never" comes when the button is pressed.. or else they will be shown normally on the action bar options menu..

Androider
  • 3,833
  • 2
  • 14
  • 24
0

if you are using appcompat library you need change

android:showAsAction="never"

to app:showAsAction="never"

example

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_view_contact"
    app:showAsAction="always|withText"
    android:icon="@drawable/ic_action_person"
    android:title="@string/action_view_contact" />
<item
    android:id="@+id/action_save_contact"
    app:showAsAction="always|withText"
    android:icon="@drawable/ic_action_add_person"
    android:title="@string/action_save_contact" />

<item
    android:id="@+id/action_change_notification"
    app:showAsAction="never|withText"
    android:title="@string/action_turn_off_notifications" />

ingyesid
  • 2,864
  • 2
  • 23
  • 21
0

Following this post, the icon appears with the following code:

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    if (menu is MenuBuilder) (menu as MenuBuilder).setOptionalIconsVisible(true)
    menuInflater.inflate(R.menu.simple_menu, menu)
    return true
}

Note: there is a lint warning that this is a restricted API that should only be used internally . . . but it works.

gMale
  • 17,147
  • 17
  • 91
  • 116
0
<?xml version="1.0" encoding="utf-8"?>
<item android:id="@+id/search"
    android:title="@string/search"
    android:icon="@drawable/ic_search"
    app:showAsAction="always"/>


<item android:id="@+id/dot"
    android:title="@string/search"
    android:icon="@drawable/ic_dot"
    app:showAsAction="ifRoom">

    <menu>
        <item
            android:id="@+id/settings"
            android:title="@string/title_settings"
            android:icon="@drawable/ic_setting" />

        <item android:id="@+id/feedback"
            android:title="@string/title_send_feedback"
            android:icon="@drawable/ic_help"/>
    </menu>

</item>