Android – How to set the text color of a closed spinner
by Riley MacDonald, August 17, 2017

The Spinner class has no methods for setting text color, so how do you set the text color of a closed spinner?
All items in a Spinner are TextViews. Using the setOnItemSelectedListener() method of Spinner you can assign a listener exposing the selected TextView. From here you can customize the TextView as desired. Here’s an example showing how to change the text and the color of the selected item.

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // Get the textView
        TextView textView = (TextView) parent.getChildAt(0);
 
        // Set the text color to white
        textView.setTextColor(Color.rgb(249, 249, 249));
 
        // Set the text
        textView.setText(getString(R.string.default_spinner_message));
    }
 
    public void onNothingSelected(AdapterView<?> parent) { }
});

Bonus Lambda / Optional Approach
For those using Retrolambda (A backport of Backport of Java 8’s lambda expressions to Java 7, 6 and 5) I used an Optional for null safety.

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Optional<TextView> spinnertext = Optional.of((TextView) parent.getChildAt(0));
 
        spinnertext.with(textView -> {
            textView.setTextColor(Color.rgb(249, 249, 249));
            textView.setText(getString(R.string.default_spinner_message);
        });
    }
}
Open the comment form

Leave a comment:

Comments will be reviewed before they are posted.

User Comments:

Walter Williams on 2019-12-30 23:17:22 said:
It appears that you are wrapping the onItemSelected with another function. I found that Android Studio 3.5.3 won't let me do this.