2

How can I always show Android settings button on ActionBar? (4.0+)

I would like to show it even if the device has an hardware button for settings, so it would be same with devices with and without hardware buttons.

This is what I'm talking about: http://oi48.tinypic.com/2j104l0.jpg

Badr Hari
  • 8,114
  • 18
  • 67
  • 100

1 Answers1

6

There is one awful hack which generally have been answered on many questions. Call this from your onCreate():

private void getOverflowMenu() {

     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) {
        e.printStackTrace();
    }
}

It is highly suggested not to use this hack as sHasPermanentMenuKey field can change anytime. However this has been working for me and others uptil now. See

Android action bar not showing overflow

On a second note, the hardware button is present on the phone for a reason. Just showing the addition overflow menu might confuse your users. Ideally a user would be very much accustomed to using his/her personal phone. So, if the user's phone has a hardware button for overflow menu option, then it need not have the icon on the top of action bar. Having an addition button might confuse users due to difference in behavior in different apps.

Hope that helps.

Edit:

In short, its not recommended as sHasPermanentMenuKey field in the Android code can be changed anytime, which will break your app if not found.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Thank you, can you explain why is it awful and if/why I should avoid doing it all together? – Badr Hari Aug 08 '13 at 19:46
  • Good explanation is here http://stackoverflow.com/questions/9286822/how-to-force-use-of-overflow-menu-on-devices-with-menu-button – tokudu Dec 06 '13 at 01:20