showAsAction="ifRoom" doesn't show the item even when there is plenty of room

Kenny Wyland picture Kenny Wyland · Apr 10, 2013 · Viewed 54.4k times · Source

I am trying to get the ActionBar working properly on my app (and I'm using ActionBarSherlock to get a unified UI between Android 2.x and 4.x).

I feel like android:showAsAction="ifRoom" is just a big, fat lie. Whenever I set an action to ifRoom it ALWAYS shows up in the overflow menu even if there is PLENTY of room. Here are two screenshots from the same emulator. The first shows the ActionBar with all options set to always and the second shows the ActionBar with the last two options set to ifRoom. As you can see, there was PLENTY of room when they were all shown in the always screenshot, so why aren't they all showing in the second because they DO have room?

enter image description here

enter image description here

Here is my menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/add"
        android:icon="@drawable/ic_menu_btn_add"
        android:showAsAction="always"
        android:title="Add"/>
    <item
        android:id="@+id/calculateNPV"
        android:icon="@drawable/menu_icon_npv"
        android:showAsAction="always"
        android:title="NPV"/>
    <item
        android:id="@+id/calculateIRR"
        android:icon="@drawable/menu_icon_irr"
        android:showAsAction="always"
        android:title="IRR/YR"/>
    <item
        android:id="@+id/send"
        android:icon="@android:drawable/ic_menu_share"
        android:showAsAction="always"
        android:title="@string/share_pdf"/>
    <item
        android:id="@+id/graph"
        android:icon="@drawable/ic_menu_gallery"
        android:showAsAction="ifRoom"
        android:title="@string/view_cashflow_diagram"/>
    <item
        android:id="@+id/deleteReorder"
        android:icon="@drawable/ic_menu_clear_playlist"
        android:showAsAction="ifRoom"
        android:title="@string/delete_reorder_cashflows"/>

</menu>

Answer

josagyemang picture josagyemang · Jun 2, 2014

I hope I am not too late in coming to the party.

It is really not a big fat lie but a small oversight.

The showAsAction attribute must be defined using a different namespace "http://schemas.android.com/apk/res-auto"

You should therefore in your top menu tag define a namespace as follows xmlns:app="http://schemas.android.com/apk/res-auto"

and then use that to define your showAsAction attribute like so app:showAsAction="ifRoom"

That should fix it