Search code examples
androidandroid-actionbarandroid-actionbar-compatandroid-optionsmenu

Why is my menu item always showing in overflow menu?


The reason the overflow dots aren't showing in the screenshot is because this emulator has a menu button. But a menu press does show the item.

NerdLauncherFragment.java:

@Override
public void onCreate(Bundle savedInstanceState) {
    … 
    setHasOptionsMenu(true);
    … 
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.nerd_launcher, menu);
}

nerd_launcher.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.bignerdranch.android.nerdlauncher.nerdlauncher.NerdLauncherActivity" >

    <item android:id="@+id/action_settings"
          android:icon="@drawable/ic_launcher"
          android:title="R"
          app:showAsAction="always" />
</menu>

enter image description here

I thought for sure I had figured it out when I made the custom namespace for showAsAction as suggested in other answers, but my item is still stuck in the overflow menu.


Solution

  • You can add an ActionBar to your activity when running on API level 7 or higher by extending ActionBarActivity and setting the activity theme to Theme.AppCompat or a similar theme.