33

I can't understand why wrong and incompatible (AndroidStudio tells me "Should use app:showAsAction with the appcompat library) code

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/action_search"
      android:title="@string/action_search"
      android:icon="@drawable/search"
      android:showAsAction="always" />
</menu>

works perfect, but proper and compatible version like

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

not showing my icon at all.

I'm testing on Samsung GT P5210 (android v. 4.4.2) and Nexus 7 (4.4.4)

2
  • Can you also provide the code you used in your Fragment/Activity?
    – Siebe
    Oct 15, 2014 at 7:57
  • Just public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; }
    – ITurchenko
    Oct 15, 2014 at 8:01

3 Answers 3

56

Things you should always check when you want to use action bar are

1) Extend ActionBarActivity instead of Activity

public class MainMenu extends ActionBarActivity{

2) Have the right style selected as defined at manifest

Manifest

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

Style

    <style name="AppTheme"
    parent="Theme.AppCompat.Light.DarkActionBar">
    </style>

3) Select the right title for showAsAction

  <menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:**yourapp**="http://schemas.android.com/apk/res-auto" >
  <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      **yourapp**:showAsAction="ifRoom"  />
    ...
  </menu>

This is what most people get wrong

4) Define your Menu in Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

If you do all the following your action bar should work.

Then you should add the onClickListener for every position...

5
  • 2
    Thanks a lot for detailed answer. In my project was wrong parent theme (android:Theme.Holo.Light instead Theme.AppCompat.Light) and Activity was extending basic Avtivity (instead ActionBarActivity). Now it works ok.
    – ITurchenko
    Oct 15, 2014 at 9:02
  • The best answer with complete steps from the right parent class, manifest, style, to menu setup. I would advise people with similar problem to just follow the step by step. Sep 29, 2015 at 5:26
  • 1
    Great answer. However I would rather use the @Override public boolean onOptionsItemSelected(MenuItem item) method than OnClickListener for each item. as described here: developer.android.com/guide/topics/ui/menus.html Nov 24, 2015 at 3:57
  • 1
    but i dont understand what to put in place **yourapp**... can you help? Jun 2, 2016 at 10:22
  • @VarunBhatia it can me anything. It can be xmlns:house="schemas.android.com/apk/res-auto" and then house:showAsAction="ifRoom"
    – marduc812
    Jun 3, 2016 at 0:03
16

I just reread your question and saw your problem is the complete opposite (but some pieces of my old answer still apply to your problem), so here is the updated answer:

Update:

You have imported the appcompat library in your gradle file, but you seem to support only devices newer than API Level 11 or 14? If this is the case, the lint check sees that you have imported the appcompat library via gradle and it thinks that you should use the ActionBarActivity because of your library import. That's why you are getting the error. But as your android:showAsAction attribute is working, you are using the native Activity and the native attribute call is correct, even if the lint check says it is wrong. So if you want to remove the lint error, you have to delete the appcompat lib from your gradle file and maybe change your activity theme to the native Holo Light theme, as your current theme might rely on the appcompat theme.

The answer why it isn't working with the app namespace is in the XML attribute loading for native respectively library code, which is handled in the old answer.

Old Answer

If you are using the ActionBarActivity from the support library to reach devices lower than API level 11, the main issue here is, that the ActionBarActivity recreates some of the native Android XML attributes such as android:showAsActionin its own scope, which you define with:

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

and then access them with the same attribute (here showAsAction) in the app: namespace. So the ActionBarActivity can't see or reach the native android:showAsAction attribute as it is only looking for it in the app namespace and not the android namespace.

If you want to use the native attribute, you have to use the native Activity with a Holo Theme, which is only supported from API Level 11 and higher.

2
  • I found this very helpful, especially about looking in the gradle file and deleting the appcompat entry. Thank you!
    – midiwriter
    Jan 9, 2015 at 0:01
  • If only I had read your new answer before wasting a few hours to figure it out myself... but it's still good to get some confirmation!
    – hubeir
    May 27, 2015 at 19:09
5

addthis:

yourapp:showAsAction="ifRoom"

or

android:showAsAction

for example:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />

</menu>

and in Activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
            openSearch();
            return true;
        case R.id.action_compose:
            composeMessage();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

And read more here

3
  • xmlns:[yourapp]="schemas.android.com/apk/res-auto"> and [yourapp]:showAsAction="always"
    – MilanNz
    Oct 15, 2014 at 8:11
  • Can you please describe more specifically what should be instead [yourapp]? Some random label? I don't see any references to application name in AndroidManifest.
    – ITurchenko
    Oct 15, 2014 at 8:14
  • " Replace [yourapp] with your app name or any namespace your heart desires everywhere. "
    – MilanNz
    Oct 15, 2014 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.