Item with app:showAsAction not showing
Asked Answered
C

3

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)

Chirr answered 15/10, 2014 at 7:55 Comment(2)
Can you also provide the code you used in your Fragment/Activity?Forsaken
Just public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; }Chirr
L
57

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

Loot answered 15/10, 2014 at 8:54 Comment(5)
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.Chirr
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.Tarrant
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.htmlAidaaidan
but i dont understand what to put in place **yourapp**... can you help?Comprador
@VarunBhatia it can me anything. It can be xmlns:house="schemas.android.com/apk/res-auto" and then house:showAsAction="ifRoom"Loot
I
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.

Interradial answered 15/10, 2014 at 8:21 Comment(2)
I found this very helpful, especially about looking in the gradle file and deleting the appcompat entry. Thank you!Towroy
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!Doctrine
X
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

Xerosis answered 15/10, 2014 at 8:0 Comment(3)
xmlns:[yourapp]="schemas.android.com/apk/res-auto"> and [yourapp]:showAsAction="always"Xerosis
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.Chirr
" Replace [yourapp] with your app name or any namespace your heart desires everywhere. "Xerosis

© 2022 - 2024 — McMap. All rights reserved.