Android: Where is the Spinner widget's text color attribute hiding?

89,334

Solution 1

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"

Solution 2

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.

Solution 3

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" />

Solution 4

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);
    }
});

Solution 5

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

Share:
89,334

Related videos on Youtube

Christopher Perry
Author by

Christopher Perry

Minimum Viable Programmer

Updated on July 05, 2022

Comments

  • Christopher Perry
    Christopher Perry almost 2 years

    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.

    • Ray Tayek
      Ray Tayek about 12 years
      there is a color and background for the text in a spinner. you can set it in SpinnerAdapter.getView().
  • Christopher Perry
    Christopher Perry almost 13 years
    Well, the reason I think it's in a style somewhere is because when I change from the Holo to the Holo.Light theme, the text color on the spinners change from white to black.
  • JoxTraex
    JoxTraex almost 13 years
    Well you could define your own style and then apply it to the spinner, right?
  • Christopher Perry
    Christopher Perry almost 13 years
    I could, but I have no clue what attribute to override for the text color, hence my question.
  • Christopher Perry
    Christopher Perry almost 13 years
    That's for the spinner items that go in the dropdown. I need it for the text in the actual spinner.
  • CaseyB
    CaseyB almost 13 years
    That is the actual spinner. I am editing my answer with the full implementation.
  • Christopher Perry
    Christopher Perry almost 13 years
    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.
  • pengwang
    pengwang almost 13 years
    can you tell me how to do custom triangle on the right of the spnner?
  • Siddhant
    Siddhant over 12 years
    For 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...
  • Christopher Perry
    Christopher Perry over 12 years
    @pengwang it's part of the nine patch background drawable for Spinner.
  • Diederik
    Diederik over 11 years
    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.
  • Vasil Valchev
    Vasil Valchev about 11 years
    this is most simplest method here :)
  • Stephen Lynx
    Stephen Lynx almost 11 years
    What if I want to change the style of some spinners and not all spinners in the application?
  • raider33
    raider33 over 10 years
    Nice example. I couldn't get the others to work in my project, but this was easy and worked like a charm.
  • An-droid
    An-droid about 10 years
    Then you only apply the theme to this one spinner
  • David Kibblewhite
    David Kibblewhite almost 10 years
    I find I have to check that parent has children first, otherwise there is a NullPointerException raised whenever i change the device's orientation.
  • Gerhard Burger
    Gerhard Burger over 9 years
    This is also a solution that doesn't force the new style globally, like the others do.
  • bkurzius
    bkurzius about 9 years
    This may be the best solution if the color is set only for this one spinner and not globally
  • Roman_D
    Roman_D over 7 years
    For AppCompat theme just remove android: like this <item name="spinnerDropDownItemStyle">@style/mySpinnerItemStyle</i‌​tem>
  • sud007
    sud007 almost 7 years
    Ideally if someone wants to reutilise their Spinner Adapter, this is the best way to do. Thanks worked that way!
  • vlasentiy
    vlasentiy almost 7 years
    NPE 'void android.widget.TextView.setTextColor(int)'
  • pollaris
    pollaris over 6 years
    Excellent! I used @android:color/holo_orange_light
  • ymerdrengene
    ymerdrengene about 6 years
    This also changes the textcolor for both item and dropdown
  • daka
    daka over 5 years
    Could you please explain a bit more about what each style is for? what makes the text red?