22

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.

Peri Hartman
  • 19,314
  • 18
  • 55
  • 101
  • 2
    "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](http://en.wikipedia.org/wiki/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. – CommonsWare Dec 07 '13 at 18:14
  • 2
    Correct: 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+). – Peri Hartman Dec 07 '13 at 18:18
  • "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). – CommonsWare Dec 07 '13 at 18:29

2 Answers2

58

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks. I looked far and wide but never came across this. – Peri Hartman Dec 07 '13 at 21:47
  • Google 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. – TheLettuceMaster Mar 26 '14 at 14:03
  • 2
    CommonsWare 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. – j2emanue Mar 26 '14 at 18:55
  • @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. – CommonsWare Mar 26 '14 at 18:58
  • It 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. – CelinHC Apr 16 '14 at 18:41
  • i get a NoSuchFieldException on android 2.3 :( and my device has a hardware menu button :( .. why could that be so ? @CommonsWare sir? – Rat-a-tat-a-tat Ratatouille Aug 02 '14 at 09:40
  • 1
    @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. – CommonsWare Aug 02 '14 at 12:56
  • @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.. – Rat-a-tat-a-tat Ratatouille Aug 02 '14 at 13:20
27

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>
Jongz Puangput
  • 5,527
  • 10
  • 58
  • 96
Rino
  • 1,215
  • 1
  • 16
  • 28
  • This 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. – thekevshow Jul 20 '14 at 23:08
  • Thank you @Rino. Your post was exactly what I was searching for. – Mustafa Chelik Dec 23 '14 at 21:43
  • very neat and clear solution. just one question, can we dynamically add menu items in that menu_overflow's menu ?? – Bugs Happen Jan 16 '15 at 06:59
  • "One drawable with 3 dots". Where does that come from? How can we be sure it looks like the default one? – BeniBela Dec 09 '16 at 00:23