6
Android Studio 0.5.8

Hello,

For some reason the icon never displays on the ActionBar, I have used a combination of ifRoom|withText but still doesn't display. I have also tried rotating in Landscape. I am using genymotion 4.4.2

<?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:title="@string/new_crime"
        android:id="@+id/menu_item_new_crime"
        android:icon="@drawable/ic_action_new"
        app:showAsAction="always"/>
</menu>

I am inflating the menu in a fragment:

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_crime_list, menu);
    }

Here is a screenshot: enter image description here

I have tried hardware nexus5 in portrait and landscape mode, but no icon.

I have also tried using the following, but didn't work either:

android:icon="@android:drawable/ic_menu_add"

Many thanks for any suggestions,

ant2009
  • 27,094
  • 154
  • 411
  • 609

3 Answers3

13

I have come across this issue once myself. Try this:

<?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:title="@string/new_crime"
        android:id="@+id/menu_item_new_crime"
        android:icon="@drawable/ic_action_new"
        android:showAsAction="always"
        app:showAsAction="always"/>
</menu>

I don't know why it would be necessary to have both, but that fixed it for me for some reason.

Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
  • Experimenting with what you said and just having this seems to work: android:showAsAction="ifRoom|withText" The reason I used the: app:showAsAction, was because Android Studio gave me an error: "should use app:showAsAction with the appcompat library with xmlns:app="http://schemas.android.com/apk/res-auto" – ant2009 May 13 '14 at 16:57
  • 1
    I ignored the error "should use app:showAsAction with the appcompat library with ..." and was able to compile the code successfully. Menu item was shown in action bar. – Manish Mulimani Jan 27 '15 at 06:03
  • Thanks a lot. Ignoring the warning, putting both android:showAsAction and app:showAsAction fixed the issue. – samir105 Apr 09 '15 at 20:12
  • It really works, but I wonder why does it work? It never mentioned anywhere in the documentation... – Oleksandr Berdnikov Dec 29 '16 at 15:11
  • @AlexBerdnikov It shouldn't work that way. I think it is a device specific issue. Some devices (mostly Samsung devices) don't follow the Android standards very well and break loads of little stuff like this. My advice: just don't worry about it if you don't have to - it's the fault of the device you are testing on, not you. – Xaver Kapeller Dec 29 '16 at 16:45
  • @XaverKapeller I wouldn't wonder in case of Samsung because it never stops surprizing you :) But the thing is that it works like that on my Xperia Z1 (Android 5.1.1) and Nexus 5x (!!!) (Android 7.0) – Oleksandr Berdnikov Dec 30 '16 at 03:03
  • 1
    @AlexBerdnikov Yeah, its a weird issue. However I have never been able to reliable reproduce it. In my years developing apps I have only come across this issue twice, once with specific Samsung devices and another time with a single HTC device, but later those same devices didn't have this problem when I was developing a different app. I never figured out what one needs to do to trigger this issue. If you come across it I think its best to remember what Romain Guy said at a fireside chat at Google IO a few years ago: "Good Luck". – Xaver Kapeller Dec 30 '16 at 03:42
2

You are using Android Studio, as explained here by blackfizz: "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."

I had the exact problem. Android Studio was giving me the error "should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto". If I changed my XML as suggested, my menus in the actionBar disappeared into the overflow. If I ignored the error, I got the expected behavior, but the error still bothered me.

The real culprits turned out to be the following lines in the file build.gradle:

dependencies {
    …
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:support-v4:22.1.1'
}

which imported the appcompat library, and caused all the trouble. Since I only targeted Android 4.4 and up, I was able to remove these two lines. Problem solved!

I wasted a few hours to figure it out myself before reading blackfizz's answer, so I am posting my answer here in hopes of saving other developers a few hours.

When you encounter a similar situation, first check your build.gradle to see if you have inadvertently imported the appcompat library.

Community
  • 1
  • 1
hubeir
  • 1,279
  • 2
  • 13
  • 24
0

You need to use Theme.Holo style and not AppCompat. In order to do so, just change the style of the application at AndroidManifest.xml If you get error:

should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto

Then you need to change the module settings:
1 - Right Click on your app and Select Open Module Settings (Or just Press F4)
2-In the depencencies, add a support module newer than V7 (for example com.android.support:support-v13:22.0.0)

in the menu.xml, dont write:

app:showAsAction="ifRoom"

but write

android:showAsAction="ifRoom"
Hesham Yassin
  • 4,341
  • 2
  • 21
  • 23