81

I'm trying to change the text color of the single item that is displayed in the spinner button after you select an item from the dropdown. I've been perusing the themes.xml and styles.xml in the Android SDK for an hour now, and I can't seem to find where the Spinner is getting the color value from.

To clarify, I'm NOT trying to change the color of a dropdown item, I'm trying to change the color of the spinner's displayed text when there is no dropdown. I guess you could call it the spinner's 'button' text.

Christopher Perry
  • 38,891
  • 43
  • 145
  • 187
  • 1
    there is a color and background for the text in a spinner. you can set it in SpinnerAdapter.getView(). – Ray Tayek Mar 03 '12 at 00:47

8 Answers8

109

I think it's probably this bit in styles.xml

<style name="Widget.TextView.SpinnerItem">
    <item name="android:textAppearance">@style/TextAppearance.Widget.TextView.SpinnerItem</item>
</style>
<style name="Widget.DropDownItem.Spinner">
    <item name="android:checkMark">?android:attr/listChoiceIndicatorSingle</item>
</style>

-= EDIT =- Here's the result: enter image description here

and here's how it's done:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MooTheme" parent="android:Theme">
        <item name="android:spinnerItemStyle">@style/MooSpinnerItem</item>
    </style>

    <style name="MooSpinnerItem" parent="android:Widget.TextView.SpinnerItem">
        <item name="android:textAppearance">@style/MooTextAppearanceSpinnerItem</item>
    </style>

    <style name="MooTextAppearanceSpinnerItem" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
        <item name="android:textColor">#F00</item>
    </style>
</resources>

Then just add this to the application tag in your AndroidManifest.xml

android:theme="@style/MooTheme"
CaseyB
  • 24,780
  • 14
  • 77
  • 112
  • That's for the spinner items that go in the dropdown. I need it for the text in the actual spinner. – Christopher Perry May 28 '11 at 02:06
  • That is the actual spinner. I am editing my answer with the full implementation. – CaseyB May 28 '11 at 02:23
  • 1
    I don't understand, because this says it's for the spinner items. How does it apply this to the spinner? I see that it actually does work, so I'm going to mark yours as the answer, but I'm really curious how this is actually working because I was looking in the Spinner code to see where the text is being displayed and was at a loss to find it. – Christopher Perry May 28 '11 at 02:30
  • can you tell me how to do custom triangle on the right of the spnner? – pengwang May 30 '11 at 05:57
  • 1
    @pengwang it's part of the nine patch background drawable for Spinner. – Christopher Perry Dec 08 '11 at 16:21
  • For [CaseyB](http://stackoverflow.com/users/236136/caseyb): **Can u please be more specific on instructions?** **Firstly**, if i insert the first two style tags in my style.xml file, it asks me for the parent.. **Secondly**, adding the three style tags given below the above screenshot and then adding adndroid:theme="@style/MooTheme" has no effect WHATSOEVER on my spinner... **Thirdly**, you haven't mentioned what kind of adapter i must have for my spinner... – Siddhant Dec 08 '11 at 07:11
  • The above works for me. But only when I do not set the "android:textColor" attribute in the application theme (MooTheme above). Any way of setting the spinner item colour different than the application SpinnerItem colour. – Diederik Oct 22 '12 at 11:11
  • What if I want to change the style of some spinners and not all spinners in the application? – Stephen Lynx Jul 05 '13 at 23:27
  • Then you only apply the theme to this one spinner – An-droid May 23 '14 at 13:55
  • Could you please explain a bit more about what each style is for? what makes the text red? – olfek Sep 09 '18 at 08:01
53

Yeah CaseyB is correct.

Here's how I set the spinner text color, a little simple example:

styles.xml

    <style name="Theme.NoTitleBar.WithColoredSpinners" parent="@android:style/Theme.NoTitleBar">
        <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
        <item name="android:spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem</item>
    </style>

    <style name="SpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem">
        <item name="android:textColor">#00FF00</item>
    </style>

    <style name="SpinnerItem.DropDownItem" parent="@android:style/Widget.DropDownItem.Spinner">
        <item name="android:textColor">#FF0000</item>
    </style>

</resources>

Then in your manifest:

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.NoTitleBar.WithColoredSpinners" >

The text on the outside of all your spinners will now be Green and the text on the dropdowns will be red.

Blundell
  • 75,855
  • 30
  • 208
  • 233
30

I did this using another simple technique,

copy the simple_spinner_item.xml and simple_spinner_dropdown_item.xml from Android res/layout folder and copy them in your project.

Then modify the following lines

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, Android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(Android.R.layout.simple_spinner_dropdown_item);
spinnerSubject.setAdapter(adapter);

as:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
spinnerSubject.setAdapter(adapter);

The rest is easy, you can now edit the simple_spinner_item.xml to edit the appearence of one visible item on the spinner widget, and edit the simple_spinner_dropdown_item.xml to change the appearance of drop down list.

For example, my activity layout contains:

<Spinner
android:id="@+id/mo_spinnerSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/spinnerset_background" />

and my simple_spinner_item.xml now contains:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="@color/custom_white"
android:textSize="16sp" />

and the simple_spinner_dropdown_item.xml looks like:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@color/custom_black"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="@color/custom_white" />
saad
  • 475
  • 7
  • 11
28

You can use setOnItemSelectedListener on Spinner object,

spinnerObject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        ((TextView)parentView.getChildAt(0)).setTextColor(Color.rgb(249, 249, 249));
        // OR ((TextView)parentView.getChildAt(0)).setTextColor(Color.RED);
    }
});
Community
  • 1
  • 1
Rukmal Dias
  • 3,242
  • 1
  • 34
  • 28
13

its very simple actually. I was looking for all over you just need to create style and set on spinner

first create your theme in Style.xml

 <style name="spinnerTheme" parent="android:Theme">
    <item name="android:textColor">@color/gray_dark</item>
</style>

then in your xml where you've set your spinner add this :

android:theme="@style/spinnerTheme"

                       <Spinner
                        android:id="@+id/spinner"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:padding="10dp"
                        android:paddingBottom="5dp"
                        android:paddingLeft="10dp"
                        android:layout_span="3"
                        android:layout_weight="1.3"
                        android:theme="@style/spinnerTheme"
                        android:textSize="12sp"
                        android:spinnerMode="dialog"
                        android:clickable="false"/>

Enjoy Coding

Pre_hacker
  • 1,352
  • 1
  • 17
  • 21
1

Can change the text colour by overriding the getView method as follows:

new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, list()){
                @Override
                public View getView(int position, View convertView, @NonNull ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);
                    //change the color to which ever you want                    
                    ((CheckedTextView) view).setTextColor(Color.RED);
                    return view;
              }
Madhur
  • 3,303
  • 20
  • 29
1

I dont think there is a color associated with the text. Its most likely predefined in the android code, might have to just make your own if you want to change the spinner's color.

This would include changing the ondraw() method and you draw the spinner how you would like it to look.

Only thing I think could potentially solve this issue is the style property of the spinner.

Source: http://developer.android.com/reference/android/widget/Spinner.html

This might help:

http://www.designerandroid.com/?p=28

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
0

this worked for me create your own layout file with a custom definition for the spinner item.

custom_spinner.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/txt_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#ffffff" />

Change the spinner item using adapter

Spinner spinner= (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.by_loc_array,R.layout.txt_spinner);
spinner.setAdapter(adapter);