3

The hack that developers have used in the past to allow a Hardware and Software menu button in the ActionBar, 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) { e.printStackTrace(); }

Results in: java.lang.NoSuchFieldException: sHasPermanentMenuKey

So the hack doesn't work anymore, an alternative? Edit: So the hack never worked for < Android 4.2, an alternative (without creating the button yourself)?

(I don't want to start another discussion as in How To Control use of OverFlow Menu in ICS about pros and cons of this approach)

Community
  • 1
  • 1
Matthias
  • 4,481
  • 12
  • 45
  • 84
  • 1
    What version of Android do you use? In 4.4.2 it is [there](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/android/view/ViewConfiguration.java/#230) but on earlier versions it isn't. – Jeroen Vannevel May 28 '14 at 17:05
  • @Jeroen Vannevel Android 2.3.5 – Matthias May 28 '14 at 17:07
  • 1
    As expected, it is [not available](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.5_r1/android/view/ViewConfiguration.java/) in 2.3.5. The first occurrence I see is in 4.0.1_r1. – Jeroen Vannevel May 28 '14 at 17:09
  • Well, for 2.3.5, there is no action bar, except via a library. Hence, you will need to find a recipe for the particular library you are using. – CommonsWare May 28 '14 at 17:36
  • @CommonsWare I use the class from the support library android.support.v7.app.ActionBar, I could create another menu, but this seems cumbersome – Matthias May 28 '14 at 17:44
  • 1
    FYI, I got this bug recently: java.lang.NoSuchFieldException: sHasPermanentMenuKey : at java.lang.Class.getDeclaredField(Class.java:596). It happened with 6 different users, all using version 4.4.2. – Yoann Hercouet May 30 '14 at 08:41

0 Answers0