Android appcompat actionbar menu item showAsAction not working-ThrowExceptions

Exception or error:

I have a menu item that is showing up on android 4.x but not on 2.x. Here is my menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android=""
xmlns:app="" >

This is my actionbar style

<style name="style1_actionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="android:background">@color/blue_dark</item>
    <item name="android:textColor">@color/white</item>
    <item name="actionMenuTextAppearance">@color/white</item>
    <item name="background">@color/blue_dark</item>

Any ideas?

Edit: removed double quote typo

Could it be the fact that I am showing only text, no icons? I’m kind of stuck here.

How to solve:

Whew, thanks for your help guys but I managed to figure it out. It wasn’t a problem with the xml, it was a problem with the onCreateOptionsMenu function.

I was using this

new MenuInflater(getApplication()).inflate(, menu); 

instead of this

MenuInflater inflater = getMenuInflater();
inflater.inflate(, menu);

Not entirely sure why this works but it does.


<menu xmlns:android=""
  **xmlns:yourapp=""** >

    <item android:id="@+id/action_search"
      **yourapp**:showAsAction="ifRoom"  />

Please refer to the documentation.

Using XML attributes from the support library

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.


In my case I had to add a few lines to onCreateOptionsMenu.

Android Studio didn’t let me use android:showAsAction=”ifRoom” while using appCompat.

app:showAsAction=”ifRoom” wasn’t working and I removed it without problems.

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater  inflater = getMenuInflater();
        inflater.inflate(, menu);
        for (int i = 0; i < menu.size(); i++) {
        return super.onCreateOptionsMenu(menu);


If you want your app to support action bar below 3.0 you need to use app compact v7 from the support library.

Also check the link


Using the menu in an activity that extends the AppCompact it is necessary import the app context in the XML and use it:

<menu xmlns:android="" 

    <!-- "Mark Favorite", should appear as action button if possible -->

    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"


What you need to do basically is add xmlns:app=""to the menu element in yout XML and use the showAsAction in the following format: app:showAsAction="ifRoom".

This will show the icon in the action bar, if possible.

Leave a Reply

Your email address will not be published. Required fields are marked *