Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Menu items not shown in menu bar

I have the same problem as posted many times like here or here. I define a menu item which shows up in the preview in AndroidStudio:

enter image description here

But when I run the app on my phone the icon (a png image) is not visible, and there is a lot of space available. However, this 'Add' option shows up in the Options menu (to the very right; together with 'Srttings'). Here is my menu.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=".MainActivity">

    <item android:id="@+id/action_favourite"
        android:icon="@mipmap/ic_add"
        android:title="@string/menu_add"
        android:showAsAction="always"/>


    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />

</menu>

I tried the suggestions I could find, but none of them solved my problem. My phone is a LG G3. How can I solve this problem?

Additional information: onCreateOptionsMenu

@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_main, menu);
    return true;
}
like image 583
Alex Avatar asked Oct 27 '15 13:10

Alex


2 Answers

Just use app:showAsAction="always" and xmlns:app="http://schemas.android.com/apk/res-auto" then it will show.

<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=".MainActivity">

    <item android:id="@+id/action_favourite"
        android:icon="@mipmap/ic_add"
        android:title="@string/menu_add"
        app:showAsAction="always"/>


    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />

</menu>

You're probably using the support library which is dependent on the app namespace. If in doubt, just add the same property twice (android: and app: namespaces).

like image 192
David Medenjak Avatar answered Sep 24 '22 10:09

David Medenjak


In onCreate() add setHasOptionsMenu(true)

And you are probably inflating the wrong menu file.

like image 42
YakuZa Avatar answered Sep 22 '22 10:09

YakuZa