android 4.0, text on the action bar NEVER shows android 4.0, text on the action bar NEVER shows android android

android 4.0, text on the action bar NEVER shows


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.

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@+id/menu_foo"          android:title="@string/menu_foo"          android:icon="@drawable/ic_menu_foo"          android:showAsAction="always"          android:actionLayout="@layout/action_button_foo" /></menu>

Then create your action_button_foo.xml layout:

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:paddingTop="14dp"    android:paddingBottom="14dp"    android:gravity="center"    android:text="@string/menu_foo"    android:drawableLeft="@drawable/ic_menu_foo"    android:background="@drawable/bg_btn_action_bar"    android:clickable="true" />

and use a selector for its background bg_btn_action_bar.xml, so it changes color when you tap it:

<?xml version="1.0" encoding="utf-8" ?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:state_pressed="true"        android:drawable="@drawable/bg_action_bar_pressed" />    <item        android:drawable="@color/transparent" /></selector>

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.

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    getMenuInflater().inflate(R.menu.my_menu, menu);    final MenuItem item = menu.findItem(R.id.menu_foo);    item.getActionView().setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {            onOptionsItemSelected(item);        }    });    return super.onCreateOptionsMenu(menu);}


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):

  1. In portrait mode, my logo/up button appears on the top row of the action bar, tabs appear on the second row, and any actions appear as icons only (no text) in the right side of the top row.
  2. But if when I rotate to landscape, the action bar collapses to a single row, and tabs move up to the top bar as a spinner (drop-down list) next to my up button. But notably, then the text appears next to my action icons.

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.


If you want your Options Menu to show up in your action bar with Honeycomb, I did this:

In your activity, override this function:

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.actionbar_universe, menu);    return true;}

where R.menu.actionbar_universe define your menu item like this:

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@+id/crossholdings" android:showAsAction="always|withText"      android:title="Cross Holdings" android:icon="@drawable/actionbar_cross"/></menu>

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.

public ActionBar setActionBarStyle(String title) {    ActionBar actionBar = setActionBarStyle();    actionBar.setTitle(title);    return actionBar;}public ActionBar setActionBarStyle() {    ActionBar actionBar = getActionBar();    ShapeDrawable actionBackground = new ShapeDrawable();    actionBackground.getPaint().setColor(Color.RED);    actionBackground.setBounds(0, 0, 5, 5);    actionBar.setBackgroundDrawable(actionBackground);    actionBar.setDisplayUseLogoEnabled(true);    actionBar.setDisplayHomeAsUpEnabled(true);    return actionBar;}