Android Spinner Text Color Size Programmatically Selected Item & Dropdown

android custom spinner with image, Android Custom Spinner Dropdown arrow background, android spinner search hint prompt text, android spinner divider, android spinner text color size. android spinner searchable, android spinner disable item dropdown, android spinner custom adapter

Read about Android Spinner Text Color Size Programmatically tutorial example here.

In this tutorial, you will learn to change the text color and size of android spinner Programmatically.

You will also learn to change the text color and size of selected item of spinner.

We will also see how to change the text color and size of dropdown text and change color size of selected item in dropdown of spinner.

For this whole purpose we will implement custom adapter to our spinner.

The Ending Scenes

Step 1. Same Text For Spinner and Drop down

In this tutorial, I will make two spinners.

In one spinner, we will set same text color and size of spinner main text and drop down text.

Another second spinner will have different color and size for spinner and drop down text.

Let us first create first spinner.

Make a new layout file in res->layout directory. Name of file should be brand_dropdown.xml

Code for brand_dropdown.xml is as below

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:id="@+id/brand_main"
    android:singleLine="true"
    android:textStyle="bold"
    android:layout_marginLeft="50dp"
    android:text="At res"
    android:textColor="#ff1020"
    android:textSize="20sp"
    android:gravity="center_vertical" />
  • This file creates a view for spinner and drop down text items.
  • You can change the text color and size of the text in the above file only.
  • Now we will see how to implement above file in java class.

Add the following code in MainActivity.java file

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

public class MainActivity extends AppCompatActivity {
  
    private String[] brands = {"Levi's","Globus","Adidas"};
    private Spinner spBrands;

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

        spBrands = findViewById(R.id.spBrands);
        // Initializing an ArrayAdapter
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,R.layout.brand_dropdown,brands);
        spinnerArrayAdapter.setDropDownViewResource(R.layout.brand_dropdown);
        spBrands.setAdapter(spinnerArrayAdapter);

    }
}
  • As you can see in above code, we have set brand_dropdown.xml using setDropDownViewResource() method.

Step 2. Spinner With Custom Adapter

For making a spinner with custom adapter first add the below code in activity_main.xml

 <Spinner
        android:id="@+id/spCars"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:spinnerMode="dropdown"/>

Write down the below source code in MainActivity.java

 public static int selectedCar=0;

    private String[] cars = {"Mercedes","Audee","Ford"};
    private Spinner spCars;
  • Add the above code before the onCreate() method.
  • I have created one integer variable which will hold the current selected position of spinner options.
  • Then a string array will contain names of the cars as a spinner options.

After the onCreate() method, add the following code

  spCars = findViewById(R.id.spCars);
        CarsAdapter adapter = new CarsAdapter(this, cars);
        spCars = (Spinner) findViewById(R.id.spCars);
        spCars.setAdapter(adapter);
        spCars.setSelection(1);

        spCars.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedCar = position;
                String item = (String) parent.getItemAtPosition(position);
                ((TextView) parent.getChildAt(0)).setTextColor(Color.parseColor("#b911bc"));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
  • In this code, we will create an object of the adapter class. (We will create adapter class after two minutes)
  • Then compiler will set the object of adapter with the spinner.
  • When the user selects specific option from the spinner, compiler will call the setOnItemSelectedListener() method.
  • In this method selectedCar variable will get the position of the selected option.
  • Then compiler will change the text color of spinner.
  • Now we need to create couple of layout files in res->layout directory.

Make a new file named cars_main.xml and write the below code in it

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:id="@+id/brand_main"
        android:singleLine="true"
        android:textStyle="bold"
        android:layout_marginLeft="50dp"
        android:text="At res"
        android:textColor="#000"
        android:textSize="20sp"
        android:gravity="center_vertical" />

</LinearLayout>
  • This file will give text color and size to the spinner main layout.
  • Prepare a new file and give it a name cars_dropdown.xml

Copy the following code in cars_dropdown.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/brand_dropdown"
        android:layout_width="match_parent"
        android:gravity="center_vertical"
        android:layout_height="60dp"
        android:textStyle="bold"
        android:paddingLeft="10dp"
        android:textSize="18sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000"/>

</LinearLayout>
  • Textview of above file will represent the text lauout of the drop down elements.
  • Change the text color and size from this file to reflect them in drop down pop menu.
  • Now let us make an custom adapter class.
  • Adapter class enable us to make a custom view for dropdown as well as for main layout of the spinner.

Prepare a new java class and give it a name CarsAdapter.java

Code structure for CarsAdapter.java may look like the following

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class CarsAdapter extends BaseAdapter implements SpinnerAdapter {
    String[] cars;
    Context context;

    public CarsAdapter(Context context, String[] cars) {
        this.cars = cars;
        this.context = context;
    }

    @Override
    public int getCount() {
        return cars.length;
    }

    @Override
    public Object getItem(int position) {
        return cars[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view =  View.inflate(context, R.layout.cars_main, null);
        TextView textView = (TextView) view.findViewById(R.id.brand_main);
        textView.setText(cars[position]);
        if(MainActivity.selectedCar == position) {
            textView.setTextColor(Color.parseColor("#bdbdbd"));
        }

        return textView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {

        View view;
        view =  View.inflate(context, R.layout.cars_dropdown, null);
        TextView textView = (TextView) view.findViewById(R.id.brand_dropdown);
        textView.setText(cars[position]);
        if(MainActivity.selectedCar == position) {
            textView.setTextColor(Color.parseColor("#4dbc11"));
        }

        return view;
    }
}
  • getView() method from the above code will create the view for spinner’s main layout.
  • getView() method will inflate the brand_dropdown.xml file to create the user interface of spinner’s main view.
  • getDropDownView() method is responsible for drop down user interface.
  • In this method, cars_dropdown.xml is inflated and dropdown will have view like this file.
  • Compiler will check one if condition. It will check whether position is selected or not.
  • If selected then it will change the text color.

You can Programmatically set the color and size of drop down texts in both methods.

Download Source Code For Android Spinner Text Color Size

[sociallocker]Download Source Code For Spinner_Text_color_size[/sociallocker]