25

I have been unable to set the color on the spinner widget. How is this styled?

SpinnerText

Community
  • 1
  • 1
skinnybrit51
  • 4,927
  • 7
  • 25
  • 28

6 Answers6

34

Try using this adapter for your spinner:

ArrayAdapter<String> adapter = 
    new ArrayAdapter<String>(Home.Home_Group, R.layout.my_spinner_style, yourstringarray)
{

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);

        ((TextView) v).setTextSize(16);
        ((TextView) v).setTextColor(
            getResources().getColorStateList(R.color.white)
        );

        return v;
    }

    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View v = super.getDropDownView(position, convertView, parent);
        v.setBackgroundResource(R.drawable.spinner_bg);

        ((TextView) v).setTextColor(
            getResources().getColorStateList(R.color.spinner_text)
        );

        ((TextView) v).setTypeface(fontStyle);
        ((TextView) v).setGravity(Gravity.CENTER);

        return v;
    }
};

Add this xml to your layout,

my_spinner_style.xml

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+android:id/text1"
        style="?android:attr/spinnerItemStyle"
        android:singleLine="true"
        android:textColor="#ffffff"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee" />

And at last,

spinner.setAdapter(adapter);
Mohi
  • 1,776
  • 1
  • 26
  • 39
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
22

Simple and crisp ....

private OnItemSelectedListener your_spinner _name= new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {

        ((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);


    }

    public void onNothingSelected(AdapterView<?> parent) {

    }
};
Ashraf
  • 3,114
  • 3
  • 23
  • 22
  • Just what I needed. If you do this in you Activity's onCreate() function though, with your_spinner.setOnItemSelectedListener(), don't forget to prevent onCreate to appear when orientation of your mobile changes with because you will get a null error otherwise. – Olivier de Jonge Nov 19 '16 at 09:00
6

The simplest form is:

m_spnDia = (Spinner)findViewById(R.id.spiDia);
TextView oTextView = (TextView)m_spnDia.getChildAt(0);
oTextView.setTextColor(Color.RED);
Pelli
  • 85
  • 1
  • 1
3

A shorter alternative to Andro'd answer is to let the ArrayAdapter create the item views for you from a layout resource:

final List<String> values = [SPINNER VALUES];
final ArrayAdapter<String> adapter = new ArrayAdapter<>(
    activity, R.layout.my_spinner_item, values);
adapter.setDropDownViewResource(R.layout.my_spinner_dropdown_item);
spinner.setAdapter(adapter);

Then style your text to suit your needs in my_spinner_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="@style/my_spinner_item_style"
/>

Note: my_spinner_dropdown_item is used when the list of choices appear

For more information read the Spinners documentation.

Ferran Maylinch
  • 10,919
  • 16
  • 85
  • 100
3

If you want to change the text color : Spinner Widgets Text Color

Else, making your own layout is the best way like JoxTraex said.

Community
  • 1
  • 1
ChapMic
  • 26,954
  • 1
  • 21
  • 20
0

Try understanding that you are using the dropdown list as provided by the defaults that are available in the SDK.

SIMPLY make your own layout with a custom adapter.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45