android – how do you set the spinner text color?-ThrowExceptions

Exception or error:

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

SpinnerText

How to solve:

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

###

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

    }
};

###

The simplest form is:

m_spnDia = (Spinner)findViewById(R.id.spiDia);
TextView oTextView = (TextView)m_spnDia.getChildAt(0);
oTextView.setTextColor(Color.RED);

###

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

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

###

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.

###

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.

Leave a Reply

Your email address will not be published. Required fields are marked *