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:
menu_exit
in the overflow menu?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>
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
}