Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

menu icon not displaying on action bar

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,

like image 477
ant2009 Avatar asked May 13 '14 16:05

ant2009


People also ask

Where is the action overflow menu?

The right-hand side of the action bar shows the actions. The action buttons (3) show the most important actions of your app. Actions that do not fit in the action bar are moved to the action overflow, and an overflow icon appears on the right. Tap on the overflow icon to display the list of remaining action views.

How do I add items to Action Bar?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

What are action bar icons?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button. In general an ActionBar consists of the following four components: App Icon: App branding logo or icon will be displayed here.


2 Answers

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.

like image 170
Xaver Kapeller Avatar answered Oct 04 '22 12:10

Xaver Kapeller


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.

like image 42
hubeir Avatar answered Oct 04 '22 11:10

hubeir