AppCompatActivity custom ActionBar buttons do not appear on Android 4.1.2

Justin Case picture Justin Case · Aug 6, 2015 · Viewed 11.9k times · Source

It works fine on 5.1, but does not on 4.1.2

What am I missing?

Here is the activity xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:myApp="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/transfer"
        android:title="@string/transfer"
        android:icon="@drawable/ic_transfer"
        myApp:showAsAction="always" />
    <item android:id="@+id/refresh"
        android:title="@string/refresh"
        android:icon="@drawable/ic_refresh"
        myApp:showAsAction="always" />
    <item android:id="@+id/about"
        android:title="@string/about"
        android:icon="@android:drawable/ic_menu_info_details"
        android:orderInCategory="100"
        myApp:showAsAction="never" />
    <item android:id="@+id/help"
        android:title="@string/title_activity_help"
        android:icon="@android:drawable/ic_menu_info_details"
        android:orderInCategory="101"
        myApp:showAsAction="never" />
    <item android:id="@+id/settings"
        android:title="@string/settings"
        android:icon="@android:drawable/ic_menu_info_details"
        android:orderInCategory="102"
        myApp:showAsAction="never" />
    <item android:id="@+id/search"
        android:title="@string/search"
        android:icon="@android:drawable/ic_menu_info_details"
        android:orderInCategory="103"
        myApp:showAsAction="never" />
    <item android:id="@+id/logout"
        android:title="@string/logout"
        android:icon="@android:drawable/ic_menu_info_details"
        android:orderInCategory="104"
        myApp:showAsAction="never" />
</menu>

And here is my activity class (witch extends another activity that extends AppCompatActivity):

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_activity, menu);
    return true;
}

I am using the following appcompat library:

'com.android.support:appcompat-v7:22.2.0'

Answer

Justin Case picture Justin Case · Aug 6, 2015

Thanks for your reply. I did what these two answers suggested:

How to force use of overflow menu on devices with menu button

Actionbar not shown with AppCompat

To summarise the solution:

  1. I changed "myApp" to "compat" in the activity xml (fixed the missing buttons)
  2. I added this code

    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
    }
    

to my base activity (which every other activity extends) which extends AppCompatActivity (fixed the missing menu)

Now it works in both 5.1 and 4.1.2