Change spinner text color in android programmatically

Set custom spinner inside font family color.

Spinner itself cannot gives us any method or attribute to change text color but when we create spinner using textview and string array then we can set text view color and that will become the whole spinner texts color. So here is the complete step by step tutorial for Change spinner text color in android programmatically.

android-project-download-code-button

How to Change spinner text color in android programmatically.

Code for MainActivity.java file.

 package com.changespinnertextcolor_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;


public class MainActivity extends Activity {

 Spinner spinnerColorChange;
 
 String[] number = new String[]{
 "ONE",
 "TWO",
 "THREE",
 "FOUR",
 "FIVE"
 };
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 spinnerColorChange = (Spinner)findViewById(R.id.spinner1);
 
 ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
 this,R.layout.custom_textview_to_spinner,number );
 
 spinnerArrayAdapter.setDropDownViewResource(R.layout.custom_textview_to_spinner);
 
 spinnerColorChange.setAdapter(spinnerArrayAdapter);
 
 spinnerColorChange.setOnItemSelectedListener(new OnItemSelectedListener() {

 @Override
 public void onItemSelected(AdapterView<?> parent, View view,
 int position, long id) {
 // TODO Auto-generated method stub
 
 Toast.makeText(MainActivity.this, spinnerColorChange.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
 
 }

 @Override
 public void onNothingSelected(AdapterView<?> parent) {
 // TODO Auto-generated method stub
 
 }
 });
 
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.changespinnertextcolor_android_examples.com.MainActivity" >

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" />

</RelativeLayout>

Code for custom_textview_to_spinner.xml file.

 <?xml version="1.0" encoding="utf-8"?>
 <TextView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:textColor="#01effe"
 android:padding="12dp" />

Screenshot:

Change spinner text color in android programmatically

Click here to download Change spinner text color in android programmatically project.

Leave a Reply

Your email address will not be published. Required fields are marked *