I am trying to use the new api's from google, specifically the action bar.
When the build was set at api 10, if I pressed the menu button, I got nice looking menu options, each with a picture and icon. When using api 14, No matter what I try, it always puts the icon in the action bar with NO text. I have tried everything I can think of. I gave it the "with text" property, changed the text to a single character (in case it was a room issue), but nothing.
I have seen this done before, even in the developer guide at android.developer, but I can't seem to find an answer as to HOW to get it to show up.
I tried many options and I came up with a simple "trick" without any weird line of code, without images. And first solution with custom actionLayout simply did not work for me with API level 10 compatibility.
If you want to display text AND icon on a small action bar it means you know you have the space, right? So you can use 2 menu items:
And choose the text action to 'ifRoom' if needed so that if you do need space, the text will go away. It WILL take some more space on the action bar though but was a good compromise for me. I end up with the following code:
(EDIT Where "pelmel" is your app name END EDIT)
And then your selection handler just has to catch both IDs :
I suspect that it was a conscious decision by the Android developers to never display a single menu item's text and icon on a narrow action bar. But if you really want to do so, you can use android:actionLayout in your menu.xml file. The Android ActionBar documentation has a slightly better explanation.
Then create your
action_button_foo.xml
layout:and use a selector for its background
bg_btn_action_bar.xml
, so it changes color when you tap it:Now you'll need to make your custom view handle click events. In your Activity, I like to do this, so that I can handle the click in
onOptionsItemSelected
along with all my other, non-custom items.The "withText" property works with most tablets, but an easy way to get icons and text on smaller devices is to add the text next to the icon as one image (PNG file). That way, both the text and icon will be seen as one icon and the whole thing will display.
You can use the original icon for tablets by using the withText property.
You have to create an extra menu folder in the res directory titled "menu-w600dp".
The optionmenu.xml in this folder will only apply to screen widths bigger than 600dp (the ones that will show the icons and text with no problems).
This is definitely the same thing I've observed on my Nexus S running 4.0.4. My app uses an action bar with several tabs that are implemented as fragments. My various fragments make adjustments to the menu options displayed on the action bar while the their tab is visible.
This appears to be a bug in ICS, because it performs consistently as follows, both on my Nexus S and in the emulator (both HVGA and WVGA800):
I noticed some other glitches with the tab spinner that lead me to believe that this little corner of ICS is a bit messy/buggy. If I tell the application to split the action bar on narrow displays (by adding
android:uiOptions="splitActionBarWhenNarrow"
in the manifest, ICS always pushes those items to the bottom bar, even though there's still plenty of room at the top. And even with the extra bar, it still doesn't display the text, just the icon.On my Xoom running 4.0.4, tabs and action items always appear the way you'd expect them to appear because there's plenty of room.
Workaround: if you really want text on the action bar in portrait mode, you need to give up the icon. Remove the icon from your menu item and the text will appear. This isn't exactly what we're after though.
I've posted a bug report here: https://code.google.com/p/android/issues/detail?id=30180.
Fixed this issue by reading "If your app is using the Support Library" section under Specify the Actions in XML.
If none of these other things work for you, this may.
If you want your Options Menu to show up in your action bar with Honeycomb, I did this:
In your activity, override this function:
where R.menu.actionbar_universe define your menu item like this:
Note the showAsAction="always|withText" and specify android:title.
If you have that and its not working please copy|paste your menu resource here.
EDIT: This answers the wrong question, but it is the original text.
I use this bit of code to set the title of the action bar, and paint it red with my companies logo. It works well in 3.0.