Android Spinner tutorial with example - Androchunk

A spinner provides a quick way to select one value from a set. Touching the spinner displays a drop-down list with all available values, from which the user can select one.

android spinner


If you have a long list of choices, a spinner may extend beyond your layout, forcing the user to scroll it. A spinner scrolls automatically, with no extra code needed.



Follow these steps to create spinner in android studio:
  1. Create a Spinner element in your XML layout
  2. specify spinners values using an array and an ArrayAdapter.
  3. Create the spinner  adapter using the Spinner Adapter class.
  4. To define the selection callback for the spinner, update the Activity that uses the spinner to implement the AdapterView.OnItemSelectedListener interface.
  5. Attach setOnItemSelectedListener to Spinner
What you’ll learn in above steps:

Create a Spinner element in your XML layout: write following code in your xml file. it will create a spinner in your layout.


Create arraylist for spinner : you’ll need data to display into  the spinner as a dropdown list so for that you’ll need create arraylist for the spinner.The following shows a simple array called spinner_items. Copy following values in the strings.xml file:


Create the spinner adapter using the Spinner Adapter class : Adapter is used as a bridge in spinner. It attach list data into spinner and create dropdown list of that data.do this is when the view is created in the onCreate() method.


Attach setOnItemSelectedListener to Spinner : till now you learn how to create spinner but you can’t handle the item selection. For that you’ll need to attach onItemSelectedListener to spinner and implement it’s two methods.

  1. onItemSelected : when item selection is changed this method call for ex:  selected another item from the list. you can write your code for specific item is selected.
  2. onNothingSelected : when no item is selected this method call for ex: when remove selected item from the list which item is selected.

If you have any questions or suggestions  please leave them in the comments.


Question of the Day

change the color of Spinner and dropdown item like this,

Colored Spinner

Solution : See the solution


Official documentation:




Post a Comment

0 Comments