Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the font color of the selection list in a spinner?

I've created a spinner, from where a user can select a value. I CAN change the textcolor of the spinner itself like shown in this picture:

enter image description here

Unfortunately when I press the spinner a list of items, to be selected, is shown, but these have a white font color on white background, and I just haven't been able to change this: enter image description here

I've googled the problem and others have experienced the same problem. None of the fixes suggested worked for me. As far as I understand I have to make a custom list of some kind, but well.. I'm not able to make it work. Any suggestions?

My code looks like this (array_spinner is a array of strings):

ArrayAdapter adapter = new ArrayAdapter(this, R.layout.row, array_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

And my row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinnerText" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true"
    android:textColor="#000000" 
    android:textSize="14dp" />
like image 560
Casper Avatar asked May 11 '11 15:05

Casper


3 Answers

Try this (untested). I'm basically reproducing simple_spinner_dropdown_item.xml but with a black text color:

  • Replace the line
    ArrayAdapter adapter = new ArrayAdapter(this, R.layout.row, array_spinner);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

with

ArrayAdapter adapter = new ArrayAdapter(this, R.layout.row, array_spinner);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
  • Add this file in res/layout, called spinner_dropdown_item.xml:
<CheckedTextView 
  android:id="@android:id/text1" 
  style="?android:attr/spinnerDropDownItemStyle" 
  android:singleLine="true" 
  android:layout_width="fill_parent" 
  android:layout_height="?android:attr/listPreferredItemHeight"
  android:textColor="#FF000000"
/>
like image 174
Aleadam Avatar answered Nov 05 '22 12:11

Aleadam


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) {
    }
};
like image 37
Ashraf Avatar answered Nov 05 '22 13:11

Ashraf


I was having this same issue. My answer isn't the most proper answer, but it's a very quick solution for those using a basic app theme.

If you are creating the arrayadapter for the spinner using

createfromResource()

Do NOT use getApplicationContext(). Declare Myclass.this instead. My text now stays the same as the basic app theme. Hopefully this will help someone else.

like image 30
stclem93 Avatar answered Nov 05 '22 13:11

stclem93