Menu icon does not appear in action bar

Android Studio 0.5.8 

Hello,

For some reason, the icon does not appear on the ActionBar, I used the ifRoom | withText but still not showing. I also tried spinning in the 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 inflate the menu in the snippet:

  @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 tried the nexus5 hardware in portrait and landscape modes, but without an icon.

I also tried using the following but did not work:

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

Thanks so much for any suggestions,

+6
source share
3 answers

I came across this question once. Try the following:

 <?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 was necessary to have both, but for some reason this fixed it for me.

+13
source

You are using Android Studio, as described here by blackfizz : "checking for lint shows that you imported the appcompat library through gradle, and it thinks that you should use ActionBarActivity because of importing your library. That's why you get an error."

I had the exact problem. Android Studio gave me the error β€œshould use the application: 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 overflow If I ignored the error, I got the expected behavior, but the error still bothered me.

The real culprits were the following lines in the build.gradle file:

 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 problems. Since I only aimed at Android 4.4 and above, I was able to delete these two lines. The problem is solved!

I spent several hours figuring this out before reading the answer on blackfizz, so I am posting my answer here in the hope of saving other developers a few hours.

When faced with a similar situation, first check your build.gradle to make sure you accidentally import the appcompat library.

+2
source

You need to use Theme.Holo style, not AppCompat. To do this, simply change the style of the application to AndroidManifest.xml If you receive an error message:

should use the application: 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 application and select "Open Module Settings" (or just press F4)
2 - In the dependencies add a support module newer than V7 (for example, com.android.support:support-v13:22.0.0)

in menu.xml, do not write:

 app:showAsAction="ifRoom" 

but write

 android:showAsAction="ifRoom" 
0
source

All Articles