Alex
Alex

Reputation: 44305

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;
}

Upvotes: 9

Views: 1117

Answers (2)

YakuZa
YakuZa

Reputation: 526

In onCreate() add setHasOptionsMenu(true)

And you are probably inflating the wrong menu file.

Upvotes: 1

David Medenjak
David Medenjak

Reputation: 34532

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).

Upvotes: 8

Related Questions