How to force action bar overflow icon to show
Solution 1
Congratulations! You won!
As of Android 4.4, the ... affordance in the action bar will be there, regardless of whether the device has a physical MENU button or not. Google's current Compatibility Definition Document now comes out a bit more forcefully against having a dedicated MENU button.
The hack that developers have used in the past, to get this behavior, is:
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
}
catch (Exception e) {
// presumably, not relevant
}
That should not be needed on Android 4.4+, though with the exception handler in place, I would not expect any particular problem if you run it and, someday, they get rid of sHasPermanentMenuKey
outright.
Personally, I still wouldn't change things on Android 4.3 and below, as I suspect it's a whack-a-mole situation, where you will replace complaints about having no menu with complaints about having duplicate versions of the same menu. That being said, since this is now the officially endorsed behavior going forward, I have no problems with developers aiming for consistency on older devices.
A hat tip to the commenter on the issue I filed regarding this, pointing out the change.
Solution 2
This could be another work around, which really helped me. Keep one drawable with three dots and give it as a menu item.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/saveDetails"
app:showAsAction="always"
android:title="@string/save"/>
<item
android:id="@+id/menu_overflow"
android:icon="@drawable/dts"
android:orderInCategory="11111"
app:showAsAction="always">
<menu>
<item
android:id="@+id/contacts"
app:showAsAction="never"
android:title="Contacts"/>
<item
android:id="@+id/service_Tasks"
app:showAsAction="never"
android:title="Service Tasks"/>
<item
android:id="@+id/charge_summary"
app:showAsAction="never"
android:title="Charge Summary"/>
<item
android:id="@+id/capture_signature"
app:showAsAction="never"
android:title="Capture Signature"/>
</menu>
</item>
</menu>
Related videos on Youtube
Peri Hartman
Software developer for many years, having done an OS for an early hand held in assembly; many projects in C and C++ mostly for Windows; front-end web server handling; in 2010 started with Java & Swing for a desktop app; currently developing an Android app.
Updated on June 21, 2020Comments
-
Peri Hartman almost 4 years
I would like to force the overflow icon to always show in the action bar (assuming there are overflow items). On models with a menu button, sometimes the overflow icon doesn't appear and users must tap the devices menu button to get the rest of the action menu items. Users keep complaining about this.
Note that for the context menu, the overflow icon always shows, regardless of whether the device has a built in menu button or not.
I realize that forcing the overflow icon to appear in the action bar would duplicate the functionality of the "physical" one. You might consider that violating Androids design guidelines. In my opinion, though, the users win. They say it's confusing and I believe they're right.
-
CommonsWare over 10 years"Note that for the context menu, the overflow icon always shows" -- there is no overflow icon for a context menu. "Users keep complaining about this" -- no, some users complain about this. You are unlikely to get positive comments regarding things that work the way users expect. I recommend that you read up on selection bias. Hundreds of millions of Android devices have MENU buttons (e.g., most Samsung phones), and users seem to use those devices without issue.
-
Peri Hartman over 10 yearsCorrect: some users. But it is awkward to see a several menu options at the top of the screen and have to remember to press the menu button at the bottom of the screen to see more. Putting the three-dot icon in the action bar makes it more obvious. As for the context menu, it absolutely does show up; simply try adding enough items to your context menu and you'll see it appear (at least for android 4.0+).
-
CommonsWare over 10 years"As for the context menu, it absolutely does show up" -- no, it does not. Perhaps you are thinking of the contextual action bar (a.k.a., action mode).
-
-
Peri Hartman over 10 yearsThanks. I looked far and wide but never came across this.
-
TheLettuceMaster about 10 yearsGoogle was brilliant for doing this. In my experience the average user did not remember the menu button was there. Especially in Samsung devices were it looked invisible when the backlight was off.
-
j2emanue about 10 yearsCommonsWare you are sure this is mandatory now ? I have a Samsung galaxy 3 with kitkat 4.4.2 and it already has a menu button in the hardware. The apps do not show automatically a 'soft' overflow button on the action bar. i still had to reflect in and change that sHasPermanentMenuKey key.
-
CommonsWare about 10 years@j2emanue: AFAIK, the Compatibility Definition Document terms only apply to new devices, not OS upgrades to existing devices. And, even if it does, it's possible that Google "grandfathers in" existing devices with a MENU key getting updates, in the interests of having apps behave the same before and after the upgrade.
-
CelinHC about 10 yearsIt works. But this code must be called before the onCreate of the activity that has the actionbar. One good place is onCreate of the class that extends Application.
-
thekevshow almost 10 yearsThis is such a better more elegant solution, and is supported on all devices. The previous comment is a poor hack, that will not function on all devices and is not something that can be expected to work with certainty at all for all android devices. Well done Rino, little things like this are just so much more important that people use and understand.
-
Rat-a-tat-a-tat Ratatouille over 9 yearsi get a NoSuchFieldException on android 2.3 :( and my device has a hardware menu button :( .. why could that be so ? @CommonsWare sir?
-
CommonsWare over 9 years@Rat-a-tat-a-tatRatatouille: Well, since that field is undocumented, it is entirely possible that some device manufacturer removed it. That's the problem with techniques like this and why I do not recommend them.
-
Rat-a-tat-a-tat Ratatouille over 9 years@CommonsWare sir, oh okay.. i was just experimenting with this as i wanted to see its effect on the lower versions and kitkat also (esp the duplicate menus) that you just mentioned. but i couldn't do so :(. i cudnt create a kitkat emulator no sys image found..
-
Mustafa Chelik over 9 yearsThank you @Rino. Your post was exactly what I was searching for.
-
Bugs Happen over 9 yearsvery neat and clear solution. just one question, can we dynamically add menu items in that menu_overflow's menu ??
-
BeniBela over 7 years"One drawable with 3 dots". Where does that come from? How can we be sure it looks like the default one?