Search code examples
androidandroid-actionbar-compat

Menu items doesn't show up on the actionbar


I am using appcompat in my app. I want the menu items to show on actionbar or at least the overflow(3 dots) to show them when there is no room. There is lot of space on the actionbar, but still they don't show up. The menu flow raises from the bottom and that too only when menu button is pressed.

menu_activity.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/menu_lang"
        android:showAsAction="always"
        android:title="@string/menu_lang"
        android:icon="@android:drawable/ic_input_lang"/>

</menu>

activity:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_activity, menu);
        return true;
    }

This post says that it not works when hardware menu button is present. But other apps are able to show items on the same device. So, that answer seems to be incorrect. Can someone please help on this?

enter image description here


Solution

  • You seem to be using the wrong menu: Your file is named "menu_activity.xml" and you inflate the menu with the Resource-Id: R.menu.reminder_menu

    The Resource name of the menu should be the same as the file name, i.e.: R.menu.manu_activity

    Try it with this again - I ran into this too once and it drove me nuts...

    Update After clarification that the above part was for obfuscation, please make sure that:

    1. You extend ActionBarActivity.
    2. You use (or extend) one of the Theme.AppCompat themes for the activity (or whole app)
    3. Because on older devices, the Actionbar related attributes are not present, make sure that all these attributes in the XML are used with a custom namespace. Here that would be the showAsAction attribute, so that the compatibility library can pick them up.

    You already had the custom namespace defined ("app", in the menu tag). You need to change the android:showAsAction tag to app:showAsAction according to the Android guide.

    Your corrected menu_activity.xml would then look like this:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" >
    
        <item
            android:id="@+id/menu_lang"
            app:showAsAction="always"
            android:title="@string/menu_lang"
            android:icon="@android:drawable/ic_input_lang"/>
    
    </menu>
    

    The extended Android guide for actionbar covers these new and nasty traps...