Question

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,

Was it helpful?

Solution

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.

OTHER TIPS

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.

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"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top