Android Spinner Custom Adapter Example Tutorial

You will look in Android Spinner Custom Adapter Example Tutorial today.

Learn to make custom adapter for spinner in this tutorial with example.

Spinner is the basic and in-built UI widget of the android system. It is used to show some options among which user has to select only one.

Generally, spinner contains only option names in terms of text format. We use arrayadapter in most of the cases to populate the spinner.

With arrayadapter you can set only text items in the dropdown menu.

But what if you want to have different color or size for each dropdown? What if you want to change the background color of dropdown item?

Here, arrayadapter can not help us. We have to create our own custom adapter and need to make separate XML layout files for user interface of dropdown.

So follow all the steps to learn about custom adapter of android spinner.

Looks of Spinner Custom Adapter

Step 1. Layout Files

In this case, we need to prepare couple of XML layout files.

These two files will help us to create custom layout and view structure for dropdown items as well as for main layout of spinner.

Make new XML file in res->layout directory. Give it a name as company_main.xml

Write the below code in company_main.xml

  • This file will create a view for main layout of spinner.
  • I have taken only one textview in this file. You can also add other UI widgets like Image.
  • You can also change the color and size of the text from above code.
  • Now make another XML file under same directory. This time set the name as company_dropdown.xml

Coding lines for company_dropdown.xml are as following

  • This file contains one textview and one simple view.
  • Textview will represent the dropdown texts. You can change size, color, style of dropdown texts using this textview.
  • You change the Background of dropdown row by changing background of this textview itself.
  • After textview, there is one simple view with some color as it’s background.
  • This view draws a divider between two rows of dropdown.

Step 2. Preparing Custom Adapter

Now it is time to make a custom adapter.

Create a new JAVA class named

Write down the following lines in

Going Deep

Checkout the below code

  • It is making objects of some string array variables and context.
  • colors and colorsback contains the text which represents the various colors.
  • We will use both of these string variables later in this class only.

Read the following getView() method

  • This method is creating the view for the main spinner layout.
  • Compiler will first inflate the company_main.xml file.
  • Than you can customize the textview of this file as per your requirements.

Attend the getDropDownView() method.

  • This method will create the view for dropdown items.
  • It will inflate the company_dropdown.xml file.
  • Compiler will set text color and background color for every row.

Here we are using those colors and colorsback named string arrays.

Step 3. Final Changes

At last, we need to add some code in main files.

Copy down the following source code in activity_main.xml file.

  • I have taken one spinner in this layout.
  • You do not need to change anything in this file. You should do all the changes in adapter class only to customize the things like color, size etc.

Now add the following code structure in

  • First of all, compiler will create one string array called “company.”
  • This string array contains the names of the companies.
  • Then compiler will create the object of the CustomAdapter class.
  • At last, just set this object to the spinner and your spinner with custom adapter is ready to take off.

Download Source Code For Android Spinner Custom Adapter

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