116

I'm using the new v7 appcompat library available starting from Android 4.3 (API level 18).

Regardless of what is specified in showAsAction for a menu item, it's not shown - it always creates the overflow menu icon, and puts even a single menu item under the menu.

Trying to add menu to an activity like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_sizes, menu);
    return true;
}

And here's my menu xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_add_size"
        android:title="@string/menu_add_item"
        android:orderInCategory="10"
        android:showAsAction="always"
        android:icon="@android:drawable/ic_menu_add" />
</menu>

Is it a bug of the new support library v7, or just something wrong with the code? I've been using the similar code with ActionBarSherlock many times before.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
Mcingwe
  • 2,070
  • 2
  • 18
  • 17

13 Answers13

314

Probably you are missing required namespace:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:[yourapp]="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/menu_add_size"
        android:title="@string/menu_add_item"
        android:orderInCategory="10"
        [yourapp]:showAsAction="always"
        android:icon="@android:drawable/ic_menu_add" />
</menu>

Replace [yourapp] with your app name or any namespace your heart desires everywhere.

Other things worth checking:

  • See if your activity class extends ActionBarActivity

Check if the issue persists.


Android reference documentation: Adding Action Buttons. Here is the relevant text:

If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.)

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • 2
    That's what I've found out as well. Thank you - definitely accepted. But the showAsAction should use that custom namespace. – Mcingwe Jul 28 '13 at 23:44
  • 5
    'yourapp' means in String.xml yourapp right? – LOG_TAG Oct 15 '13 at 05:58
  • 4
    Can someone explain WHY this works? Why is there a need to extend a custom namespace? Little confused. – dineth Nov 08 '13 at 01:45
  • 2
    I'm FAIRLY CERTAIN, [yourapp] can be ANYTHING. After I got my app working using your suggestion, I tried the following: xmlns:cheese="http://schemas.android.com/apk/res-auto" up at the top, and then in the item I used: cheese:showAsAction="always". So... unless cheese is somehow defined in my project (which it isn't), then it doesn't matter what you put, as long as you're consistent :-D – Jared Nov 10 '13 at 05:57
  • 8
    @dineth appcompat is used as a library project. This means all its resources (strings, drawable, attributes...) are declared in your application namespace instead of the android namespace. As the `showAsAction` attribute did not exist in android-7, you have to use your app namespace – nicopico Nov 14 '13 at 16:53
  • 22
    Everyone says "custom namespace" or "a namespace that matches your app". But the namespace is always `http://schemas.android.com/apk/res-auto` in every example. That's not a custom one, or one that matches your app. That's the actual namespace from the documentation. I think everyone means a custom *prefix*, and it can actually be whatever you want. Is this true? – Andrew Arnott Jan 01 '14 at 18:36
  • How can I understand my namespace? I don't know how can I learn my namespace – user_vgizy Aug 19 '14 at 21:35
  • I have did all these, still getting error. Here is my question : http://stackoverflow.com/questions/27443363/error-should-use-androidshowasaction-when-not-using-support-library – Uniruddh Dec 13 '14 at 07:59
  • Anyone knows how to ensure to display two `MenuItem` when you create them programmatically? (Using `setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)`) I need to always display two menu items. The first one is displayed, the second one is collapsed. – grandouassou Jan 11 '15 at 20:42
  • "If you use ActionBarActivity, you can’t use the Holo or Material themes.." What if I want to use Holo or Material themes? – temirbek Aug 29 '16 at 09:56
  • Thanks..you saved my Time...+1 vote for your answer. – Sagar Aghara May 09 '17 at 06:46
52

Figured out myself. With the support library v7 the showAsAction should go under a custom namespace like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:balloonberry="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/menu_add_size"
        android:title="@string/menu_add_item"
        android:orderInCategory="10"
        balloonberry:showAsAction="always"
        android:icon="@android:drawable/ic_menu_add" />
</menu>
Mcingwe
  • 2,070
  • 2
  • 18
  • 17
35

Also make sure that you use correct inflater in ActionBarActivity.onCreateOptionsMenu() method.

Correct solution:

MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_example, menu);

Incorrect solution:

MenuInflater menuInflater = new MenuInflater(this);
menuInflater.inflate(R.menu.menu_example, menu);
petrnohejl
  • 7,581
  • 3
  • 51
  • 63
  • 1
    I have passed all the above answers and another answers on stackoverflow, but the only thing that I was missing is you answer - getMenuInflater(). Thanks for sharing this. – Amt87 Dec 03 '15 at 14:28
  • Thank you SO MUCH for this one. I converted all my 100+ menus to use a custom namespace and 1 stinkin' screen still wasn't working...turns out that 1 activity was using "new MenuInflater" instead of getMenuInflater(). Thanks a ton! – DiscDev Dec 15 '15 at 19:02
  • this is very important. I was using menuInflater creation (2 way). Thanks – wtk Mar 10 '18 at 08:20
  • thanks for this, really helped me. Btw, I have added additional answers for Kotlin users for this solution, you can check it below. – Yodi S. Mar 05 '22 at 04:43
26

For Fragments

Menus with custom namespace will prevent showAsAction from showing.

Using "android:" prefix for showAsAction will work, even though Android Studio will remark you should use a custom name space.

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_add_checkin"
          android:title="Add Checkin"
          android:orderInCategory="10"
          android:showAsAction="always"
        android:icon="@android:drawable/ic_menu_add"/>
</menu>

This is using Android SDK 22 and Support v4 fragments, in case that makes any difference.

Baker
  • 24,730
  • 11
  • 100
  • 106
  • That's the only thing that solved My Problem (though it's showing this error). Thanks ! – user2630165 Oct 30 '15 at 11:20
  • 3
    You can suppress the error/warning in XML layout with tools:ignore="AppCompatResource" within the . Docs here: http://tools.android.com/tips/lint/suppressing-lint-warnings – Baker Oct 30 '15 at 11:33
  • Your answer and Anton Kizema's are the right solutions for me. And also thanks for the tip `tools:ignore`. – hata Jun 02 '16 at 14:13
17

Got the same problem, but on Android 5. I have 3 items but OS ignored my attribute "always" and showed only 2 items. Here my solution:

  @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    Log.d(TAG, "onCreateOptionsMenu()");
    inflater.inflate(R.menu.your_menu, menu);
    for (int j = 0; j < menu.size(); j++) {
        MenuItem item = menu.getItem(j);
        Log.d(TAG, "set flag for " + item.getTitle());
        item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
    }
}
Lukas
  • 1,216
  • 12
  • 25
6

Also make sure that you have the correct path for the namespace. It will not give you an error message if it's wrong.

I had

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

instead of

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

All I knew was that it wasn't working. Not sure how I managed to forget the /apk part of the path, but it happened. No error message, just an elusive bug to track down.

BeccaP
  • 1,452
  • 1
  • 13
  • 19
6

In my case, I had to remove from my app's build.gradle compile 'com.android.support:appcompat-v7:21.0.3'.

Notice: My min sdk = 14, and created project by android studio inserted my unnesessary dependancy.

After this replace you can write android:showAsAction="always"

Anton Kizema
  • 1,072
  • 3
  • 13
  • 27
  • Thanks it solved me out! This days maybe the best solution in most cases, because it's unnecessary to support Android versions prior to 4.0. – pinyin_samu Aug 20 '15 at 11:04
  • Your answer and Baker's are the right solutions for me. – hata Jun 02 '16 at 14:13
4

This might not be your case but I was using

new MenuInflater(this).inflate(R.menu.my_menu, menu);

changing it to

getMenuInflater().inflate(R.menu.my_menu, menu);

fixed the problem!

Bakhshi
  • 1,299
  • 16
  • 25
0
<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item
        android:id="@id/back"
        android:icon="@drawable/back"
        app:showAsAction="always"
        android:title="@string/back"/>
    <item
        android:id="@id/save"
        android:icon="@drawable/le_top_btn_icon_add"
        app:showAsAction="ifRoom"
        android:title="@string/save"/>
</menu>

don't work, with supportLibraryVersion = '25.1.0' compileSdkVersion = 25

see the "Warning"

Should use app:showAsAction with the appcompat library with xmlns:app="http://schemas.android.com/apk/res-auto" less... (Ctrl+F1)

When using the appcompat library,menu resources should refer to the showAsAction in the app: namespace, not the android: namespace.

Similarly,when not using the appcompat library, you should be using the android:showAsAction attribute.

I think the warn can be ignore.

Bill
  • 1,268
  • 14
  • 14
0

add custom namespace like this to showAsAction and actionViewClass:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search"
    android:title="@string/search"
    android:icon="@drawable/ic_search"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="android.widget.SearchView" />

0

I have solved it by replacing

android:showAsAction="ifRoom"

with

app:showAsAction="ifRoom"

That is menuitme xml look like,

<item android:id="@+id/action_refresh"
      android:title="Refresh"
      android:icon="@drawable/refresh2"
      app:showAsAction="ifRoom" />
Haris
  • 13,645
  • 12
  • 90
  • 121
0

The simplest way is Modify your code by adding

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

and change this code

android:showAsAction="always"

to

app:showAsAction="always"

and finally

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/menu_add_size"
        android:title="@string/menu_add_item"
        android:orderInCategory="10"
        app:showAsAction="always"
        android:icon="@android:drawable/ic_menu_add" />
</menu>
Abdulhakim Zeinu
  • 3,333
  • 1
  • 30
  • 37
0

In Addition to @petrnohejl 's answer, in Kotlin you can just use this one line without initializing it first.

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.options_menu, menu)
        return true
    }

In my case, the menu item just ignored my app:showAsAction="always" or "ifRoom" and it really annoys me for hours. Then, after I searched in StackOverflow, I realized that I'm not using the correct MenuInflater.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Yodi S.
  • 106
  • 1
  • 5