Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the Spinner font color?

Tags:

I'm having an issue with the Droid X phones where users say that the font color turns out to be white in the spinner, making it invisible unless the users highlight the items. No other phones seem to have this problem. I was going to try to force the font to be black to see if that helps. How can I do that?

Here's how I'm currently populating the spinner. It seems like the simple_spinner_item is broken on Droid X's.

String spin_arry[] = new String[str_vec.size()]; str_vec.copyInto(spin_arry); ArrayAdapter adapter =     new ArrayAdapter(this,android.R.layout.simple_spinner_item, spin_arry); 
like image 478
JonF Avatar asked Dec 05 '10 22:12

JonF


People also ask

How do I change text color in ArrayAdapter?

Just create an anonymous subclass of ArrayAdapter and override getView(). Let super. getView() handle all the heavy lifting. Since simple_list_item_1 is just a text view you can customize it (e.g. set textColor) and then return it.

What is custom spinner in Android?

Spinner is a widget that is used to select an item from a list of items. When the user tap on a spinner a drop-down menu is visible to the user. In this article, we will learn how to add custom spinner in the app.


1 Answers

I'm going to use Spinner project sample from Android SDK for next code examples.


Code:

First, you need to create you custom adapter which will intercept the creation of views in drop down list:

static class CustomArrayAdapter<T> extends ArrayAdapter<T> {     public CustomArrayAdapter(Context ctx, T [] objects)     {         super(ctx, android.R.layout.simple_spinner_item, objects);     }      //other constructors      @Override     public View getDropDownView(int position, View convertView, ViewGroup parent)     {         View view = super.getView(position, convertView, parent);          //we know that simple_spinner_item has android.R.id.text1 TextView:                   /* if(isDroidX) {*/             TextView text = (TextView)view.findViewById(android.R.id.text1);             text.setTextColor(Color.RED);//choose your color :)                  /*}*/          return view;      } } 

Then you create adapter in your code like this:

 String [] spin_arry = getResources().getStringArray(R.array.Planets);          this.mAdapter = new CustomArrayAdapter<CharSequence>(this, spin_arry); 

Explanation:

Because CustomArrayAdapter knows that we use android's built-in layout resource, it also knows that text will be placed in TextView with id android.R.id.text1. That's why it can intercept the creation of views in drop down list and change text color to whatever color is needed.


Screenshot:

enter image description here

like image 103
inazaruk Avatar answered Dec 02 '22 05:12

inazaruk