Android – how do you set the spinner text color

androidandroid-widget

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

SpinnerText

Best Solution

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