How to change Spinner text color in Android

One of the questions I see beginner android developers ask is how to change Spinner text color in android. If you want to customize or change spinner text color, you can achieve it in one of the following ways listed below.

Change Spinner Text Color in XML layout

To achieve spinner text color change in android, we will change the color of each spinner item text in the drop down resource layout file.

activity_spinner_text_color.xml

<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="24dp"
    tools:context=".spinner.SpinnerActivity">

    <android.support.v7.widget.AppCompatSpinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:layout_marginTop="30dp" />

</android.support.v7.widget.LinearLayoutCompat>

spinner_text_color.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    android:textColor="@color/colorAccent"/>

SpinnerTextColor.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.inducesmile.inducesmile_button.R;

public class SpinnerTextColor extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_text_color);

        Spinner spinner = findViewById(R.id.spinner);

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.social_media, android.R.layout.simple_spinner_item);

        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(R.layout.spinner_text_color);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);
    }
}

Change Spinner Text Color Programmatically in Android

SpinnerTextColor.java

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.inducesmile.inducesmile_button.R;

public class SpinnerTextColor extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_text_color);

        Spinner spinner = findViewById(R.id.spinner);

        String [] dataSource = getResources().getStringArray(R.array.social_media);
        CustomArrayAdapter<CharSequence> customArrayAdapter = new CustomArrayAdapter<CharSequence>(this, dataSource);

        // Specify the layout to use when the list of choices appears
        customArrayAdapter.setDropDownViewResource(R.layout.spinner_text_color);
        // Apply the adapter to the spinner
        spinner.setAdapter(customArrayAdapter);
    }


    static class CustomArrayAdapter<T> extends ArrayAdapter<T> {
        
        public CustomArrayAdapter(Context ctx, T[] objects) {
            super(ctx, android.R.layout.simple_spinner_item, objects);
        }


        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            TextView text = (TextView) view.findViewById(android.R.id.text1);
            text.setTextColor(Color.BLUE);

            return view;

        }
    }
}

If you know any other means on how to change spinner text color in android kindly use the comment box below to share your source.

 

Add a Comment