Android Spinner Text Color Size Programmatically Selected Item & Dropdown

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.

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

  • 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

  • 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

Write down the below source code in MainActivity.java

  • 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

  • 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

  • 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

  • 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

  • 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.

The End

Download Source Code For Android Spinner Text Color Size

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

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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