Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text color of a closed spinner

Tags:

I understand that the closed spinner is actually a View, I think. But I am guessing it has a TextView there somewhere to show the text. How do I get access to that TextView so I can change the textcolor?

EDIT: I need to change this programatically on the fly, not in the XML.

TextView v = (TextView) getView(mySpinner);  v.setTextColor(..... 

This doesnt work...

Thank you!

    array_typ=new String[5];     array_typ[0]="Pressure";     array_typ[1]="Level";      array_typ[2]="Overage";     array_typ[3]="Under";     array_typ[4]="Taken";       adaptertyp = new ArrayAdapter<Object>(this,R.layout.simple_spinner_item, array_typ);     typ.setAdapter(adaptertyp); 
like image 513
Mark Worsnop Avatar asked Feb 02 '11 22:02

Mark Worsnop


People also ask

What property is used for text color in Android?

In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000" .

What is spinner in Android with example?

Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.


2 Answers

To modify the text color create a new xml file in your res/layout folder (for example my_spinner_text.xml). Add a text view to the new xml file and modify how you want:

<TextView android:id="@+id/spinnerText"      android:layout_width="fill_parent"     android:layout_height="wrap_content"      android:layout_centerHorizontal="true"     android:textColor="#CCCCCC"      android:textSize="20dp"      xmlns:android="http://schemas.android.com/apk/res/android"/> 

Create an ArrayAdapter that uses the new TextView and set it to your spinner:

    Spinner localSpinner = (Spinner)findViewById(R.id.mySpinner);     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,                 R.array.spinner_array,                 R.layout.my_spinner_text);     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     localSpinner.setAdapter(adapter); 
like image 90
Vito Avatar answered Sep 20 '22 08:09

Vito


You can change the text colour or can access the textview in setOnItemSelectedListener event,

            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));                 } 
like image 39
Rukmal Dias Avatar answered Sep 18 '22 08:09

Rukmal Dias