Android Spinner Divider Color Style Size Programmatically

Android Spinner Divider Color Style Size Programmatically Tutorial is the topic of the day.

Spinner is a UI widget using which you can fetch the selection of the user among different options.

You can set options in spinner by two methods : In drop down manner and in pop up dialog manner.

In this tutorial, I will explain three methods using which you can set divider between two spinner options.

Generally, divider is a line with different colors which separates two options from each other.

You can set the height and color of the divider as per your requirements.

One can also use gradient effect using multi color to make attractive dividers.

Let is make three spinners with three different colored dividers.

Step 1. Using Drop down View Resource

Make a new project in android studio. I recommend to make project with “Empty activity.”

Then add the below lines in your activity_main.xml file

  • You have one spinner in layout file.
  • Now create a new XML file under res->drawable directory.
  • Set the name of this file as games_border.xml

Write down the following source code in games_border.xml

  • Above file will create one view which we will use in our spinner. This view contains a divider like strip at the bottom.
  • Prepare a new file in res->layout directory with name games_dropdown.xml.

Add the below code in games_dropdown.xml

  • This file represents the main view for the spinner.
  • The background of this file is referencing the games_border.xml
  • We will use this file in the java file.

Now write the below code in MainActivity.java

  • In the above code, we have first define the object of the spinner.
  • I have also defined a string array which holds the names of the games which are the options of the spinner.
  • Now first we will create the object of the ArrayAdapter (spinnerArrayAdapter).
  • We will use setDropDownViewResource() method to set the layout file games_dropdown.xml as a main view file.

Step 2. Give style to spinner

Second method to customize divider is using the style method.

Write the following code in res->values->styles.xml file

  • You can set the divider height and divider color in this file.

Add the below code in activity_main.xml

To set the adapter of this spinner, add the below code in MainActivity.java file

Write the above code before the onCreate() method.

Adapter related lines are as below.

  • Add the above lines after onCreate() method.

Step 3. Divider with Custom Adapter

Last method is to create an adapter with custom views.

You can change views and colors easily using this method.

Add the below code in activity_main.xml

  • Make a new file under res->drawable directory.
  • Set the name of the file as gradient_divider.xml

Write down the below coding lines in gradient_divider.xml

  • Above file will create a view with gradient effect.
  • You can use three different colors to make attractive gradient colors view.
  • Now in the res->layout directory, prepare a new file named brand_main.xml

Source code lines for brand_main.xml file is as follow

Make another file in the same directory.

This time, name of this file should be brand_dropdown.xml

Let us create adapter class. For this, make a new java class named Spinner_Adapter.java

Code snippet for Spinner_Adapter.java is as following

  • In the above code, getView() method creates a view for the spinner main layout.
  • getView() method uses brand_main.xml file making new view.
  • getDropDownView() method will prepare a view for the drop down options.
  • It will use brand_dropdown.xml file to make a view for options of the spinner.

Now in MainActivity.java file, add below lines before onCreate() method.

Add the below lines inside the onCreate() method.

I have just created an object for the adapter class and have set this object to the spinner.

So the final code for activity_main.xml is

And for all the code of MainActivity.java is as below

More Spinner Tutorials

Last View

Download Source Code For Android Spinner Divider

[sociallocker]Download Source Code For Spinner_Divider[/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 *