Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change the text color with Android Action Bar drop-down navigation

I'm using Drop-down Navigation with my Action bar, and I can't get a sensible color for the corresponding text when using a dark action bar. The action bar itself is a dark grey, and the text color is black, so it's very hard to read.

I followed the basic instructions on the Action Bar developer's guide so my code is simply

SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list, android.R.layout.simple_spinner_dropdown_item);
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        // TODO
        return false;
    }
};
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);        
actionBar.setListNavigationCallbacks(mSpinnerAdapter, navigationListener);

and my styles.xml was simply

<resources>
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    </style>
</resources>

I think that should have given me something sensible, but as the following image shows, it comes out as black text on a dark grey background so it's no good.

Image of menu with black text on a dark grey background

I've tried playing around with the styles.xml file to try and solve the problem. My best attempt was the following

<resources>
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <item name="android:actionDropDownStyle">@style/myActionDropDownStyle</item>
        <item name="android:actionBarStyle">@style/myActionBar</item>
    </style>

    <style name="myActionDropDownStyle" parent="android:style/Widget.Holo.Light.Spinner">
        <item name="android:background">@color/LightCyan</item>
    </style>

    <style name="myActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">    
        <item name="android:titleTextStyle">@style/myActionBar.titleTextStyle</item>
    </style>

    <style name="myActionBar.titleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title.Inverse">
        <item name="android:textColor">@color/Orange</item>
    </style>
</resources>

Best attempt at making the items readable in the navigation dropdown

which was (1) able to change the text color of the app title to Orange, (2) able to change the background color of the selected navigation item in the action bar to a light blue, (3) able to change the background color of the dropdown so that at least the black text is shown against a light background. However nothing I do changes the text color itself, either for the selected item shown in the action bar, or for the items shown in the dropdown. My best attempt also looks horrible :-(!

All I want is a dark action bar with a sensible text color for the selected item shown in the action bar, which should be the same text color used when an item is being selected, and with the background color for the dropdown when an item is being selected being the same color as the action bar. But I can't work out how to do it, can anyone help?

FYI, I'm using Eclipse with an adt-bundle that I downloaded very recently (adt-bundle-windows-x86_64-20130219). When I search for solutions, I find a lot about the Sherlock action bar, however the styles for Sherlock weren't included in that adt-bundle. In any case, surely what I want should be possible with the Holo.Light.DarkActionBar that I'm using?

like image 223
Stochastically Avatar asked Apr 11 '13 11:04

Stochastically


People also ask

How do I change the color of the menu text?

Android App Development for Beginners This example demonstrates how do I change the text color of the menu item in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I change the color of my action bar support?

Just go to res/values/styles.edit the xml file to change the color of action bar.

How do I change the text color of popup menu in android programmatically?

The code used is: PopupMenu popupMenu = new PopupMenu(mContext, mImageView); popupMenu. setOnMenuItemClickListener(MyClass. this); popupMenu.


2 Answers

Please forgive my necromancy but this really bugged me today and the solution is actually quite quick and requires no custom adapters or styling.

If you change the context in the SpinnerAdapter's constructor to use getActionBar().getThemedContext() rather than this it will show light text on a dark background that will match the Action Bar's style.

SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActionBar().getThemedContext(), R.array.action_list, android.R.layout.simple_spinner_dropdown_item);
like image 55
Barry Avatar answered Sep 19 '22 00:09

Barry


I've worked out that I can alter the colors for the drop-down Navigation in code, by creating my own subclass of ArrayAdapter as follows:

public class myArrayAdaptor<T> extends ArrayAdapter<T> {

    public myArrayAdaptor(Context context, int textViewResourceId, T[] objects) {
        super(context, textViewResourceId, objects);
    }

    public static myArrayAdaptor<CharSequence> createFromResource(Context context, int textArrayResId, int textViewResId) {
        CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
        return new myArrayAdaptor<CharSequence>(context, textViewResId, strings);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return SetColourWhite(super.getView(position, convertView, parent));
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return SetColourWhite(super.getView(position, convertView, parent));
    }

    private View SetColourWhite(View v) {
        if (v instanceof TextView) ((TextView)v).setTextColor(Color.rgb(0xff, 0xff, 0xff)); // white
        return v;
    }
}

Then with the simple styles.xml

<resources>
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    </style>
</resources>

I can substitute the class myArrayAdaptor

SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list, android.R.layout.simple_spinner_dropdown_item)

and the text color will always be white. But isn't there a simpler way of doing this using settings in the styles.xml file?

like image 26
Stochastically Avatar answered Sep 21 '22 00:09

Stochastically