showAsAction="ifRoom" doesn't show the item even when there is plenty of room
Solution 1
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
Solution 2
It's because there is specified maximum number of items that should go to actionbar and it seems to be 4. Of course you can force them to appear by setting showAsAction: always
but regarding to google API guides:
If you believe that more than four of your menu items can be justified as action items, then you should carefully consider their relative level of importance and try to set no more than four as action items (and do so using the "ifRoom" value to allow the system to put some back in the overflow menu when space is limited on smaller screens). Even if space is available on a wide screen, you should not create a long stream of action items that clutter the UI and appear like a desktop toolbar, so keep the number of action items to a minimum.
Additionally, the following actions should never appear as action items: Settings, Help, Feedback, or similar. Always keep them in the overflow menu.
Solution 3
To complement the answer from Michal Z.: The Android Design Guide Page Patterns->Actionbar says the following in the chapter "Action Buttons":
http://developer.android.com/design/patterns/actionbar.html
How many actions will fit in the main action bar? Action bar capacity is controlled by the following rules:
- Action buttons in the main action bar may not occupy more than 50% of the bar's width.
- Action buttons on bottom action bars can use the entire width.
- The screen width in density-independent pixels (dp) determine the number of items that will fit in the main action bar:
- smaller than 360 dp = 2 icons
- 360-499 dp = 3 icons
- 500-599 dp = 4 icons
- 600 dp and larger = 5 icons
Solution 4
use app:showAsAction="always"
instead of android:showAsAction="always"
Solution 5
Use
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
Kenny Wyland
Kenny is a successful iPhone and Android app developer, a real estate investor and a life-long geek. #SOreadytohelp
Updated on July 14, 2022Comments
-
Kenny Wyland almost 2 years
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 toifRoom
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 toalways
and the second shows the ActionBar with the last two options set toifRoom
. As you can see, there was PLENTY of room when they were all shown in thealways
screenshot, so why aren't they all showing in the second because they DO have room?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>
-
Jake Wharton about 11 yearsIt's not always 4. See this chart: developer.android.com/design/media/action_bar_pattern_table.png
-
Kenny Wyland about 11 yearsBut as you can see, there IS room for those actions, so -ifRoom- is a big, fat lie. :) It's also frustrating that there is no visible indicator that there ARE more options available via the Menu button. It's much worse to show them SOME options but not all without an indicator, because showing SOME tells the user that this is the location to look. Why would there be another location?
-
Michał Z. about 11 years@Jake Wharton: Yeah right! I knew that I have seen something like this before but I couldn't remember where. Now I think that it was in one of google design in action videos about ActionBar... Thanks!
-
Michał Z. about 11 years@Kenny Wyland: watch this youtube.com/watch?v=jes1iXRFRw0 it should explain some things. There can be other things on ActionBar than "buttons". I agree about the thing with physical menu button and that there is no indicator that there are more items.
-
arne.jans over 10 yearsJake Wharton: for context and rules quoted in my answer, I give the link to the relevant page from the design guide: developer.android.com/design/patterns/actionbar.html
-
KurtCobain almost 10 yearsI had the exact same issue - and in fact in my case - I had to do exactly opposite: use android:showAsAction namespace to fix this issue.
-
nickromano about 9 yearsIf you are using
ActionBarActivity
you need to use the namespace. UsingActivity
you can use android:showAsAction -
The Original Android almost 9 yearsI tried it, did not work for me. Android documentation does not even support it.
-
SuperBiasedMan almost 9 yearsPlease explain your answer, as code only answers are usually unclear and harder to understand.
-
Derek 朕會功夫 almost 9 years@KurtCobain Using the
android
namespace fixes my problem! -
Soren Stoutner over 7 yearsIf you are using the
appcompat
library you need to useapp
. Otherwise you should useandroid
. -
Soren Stoutner over 7 yearsThe chart at developer.android.com/design/media/action_bar_pattern_table.png shows that the maximum number of
showAsAction:ifRoom
icons is 4 with the options overflow icon at the right taking up the fifth position. developer.android.com/design/patterns/actionbar.html Perhaps they will update Android to allow more icons with the introduction of the Google Play store to Chrome OS but I'm not holding my breath. -
Quimbo over 5 yearsInsted of use android:actionProviderClass="android.widget.ShareActionProvider"/> Use app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>