Android Spinner

| Page Views: 1732

Spinner is a view that displays items in a dropdown fashion, allowing user to pick one at a time.

Android Spinner

Spinner is an AdapterView. This means that it relies on an Adapter for its data.

Adapters act as the bridge between adapterviews and the underlying data source.

This makes Spinner like other adapterviews decoupled from data source. Hence we can customize the views shown in the spinner without affecting data.

Spinner is a concrete public class residing in the android.widget package.

package android.widget;

Spinner is a public class that's why we can access and use it.

public class

Public classes are visible even from other packages apart from those they've been defined.

Spinner class inherits from an abstract class called AbsSpinner. This class also resides in the android.widget package.

AbsSpinner provides to Spinner much of the capabilities it has. For example, Spinner is an adapterview since it derives from the AbsSpinner. This allows Spinner to set and return a SpinnerAdapter.

How to Define Spinner in Layout

We can use the Spinner object to specify the spinner in an xml Layout. This pattern of specifyng an Object in the Layout resource then referencing from the Java/Kotlin code is common in android as a platform and is also the one recommended.

Here are some of it's advantages:

No. Advantage
1. Declarative creation of widgets and views allows us to use a declarative language XML which makes is easier.
2. It's easily maintanable as the user interface is decoupled from your Java logic.
3. It's easier to share or download code and safely test them before runtime.
4. You can use XML generation tools to generate XML as an alternative to Android Studio.
    android:layout_height="wrap_content" />

How to Populate Spinner

Then in our java code we can first reference the Spinner from our Layout specification using the findViewById() method our activity class.

Spinner spinner = (Spinner) findViewById(;

We can then instantiate a SpinnerAdapter(e.g ArrayAdapter). We use the default spinner Layout(android.R.layout.simple_spinner_item).

We are using a static method called createFromResource(). This method is provided by the ArrayAdapter class and it returns us an ArrayAdapter instance from the external resource we provide it as our data source.

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.galaxies_array, android.R.layout.simple_spinner_item);

You have to make sure you've added string array into your strings.xml file:

<?xml version="1.0" encoding="utf-8"?>
    <string-array name="galaxies_array">
        <item>Milky Way</item>

ArrayAdapter provides us the createFromResource() method which gives us an instance of the ArrayAdapter from the string array we provided.

After that:


This method allows us specify the Layout the adapter will use to display the list of spinner choices. We use the one provided by the Android Platform.

Finally we set the adapter:


Top Android Spinner Examples

Let's look at some examples.

Hello guys.How do you do? This is what we do:

  1. Populate ArrayList with Person objects.
  2. Pass this ArrayList to our ArrayAdapter.
  3. Set our adapter to our Spinner
  4. Handle ItemSelection events,hence show a toast.

Hello friends.We have a look at android simple Spinner and see how to load data from a simple arraylist with help of arrayadapter.We also see how to handle ItemSelected events.

Android Simple Spinner CRUD tutorial. Hello.This is what we do in this example:

  • Perform basic crud operations for a Spinner.
  • We are using an arraylist as our data source.
  • We add,update and delete.

Android Spinner with CardViews with Images and text tutorial.

In this tutorial we'll look at a custom Spinner with cardviews. The cardviews will have both images and text. We'll also see how to handle the selected item click listener for our Spinner.

This is a tutorial for population of spinner from a Kotlin Array and handling the spinner's itemSelection event.

Spinner is a widget, of course defined in the android.widget package and allows us render items in a dropdwon fashion.

In this tutorial we want to see how to set array of items...

A spinner is an android widget that basically displays items in dropdown. Thus allowing ussers to select only one item at a time.

The parent class of the inbuilt android spinner is the AbsSpinner, a an abstract adapterview.

However, sometimes we are not so much attracted to the inbuilt or default spinner, due to functionality limitations or we just want something nicer.

In that case we can look at GitHub and obtain a third party library.

Material Spinner is such library. It was first created by Jared Rumler more than 2 years ago. Since then it's been receiving updates.

Android SearchableSpinner Library Tutorial and Example.

What about if you have a spinner with plenty of items and you want to provide users with ability to search or filter those items.

What do you do then?

Well you have a solution is SearchableSpinner by @michaelprimez in SearcableSpinner.

Android MaterialSpinner Library and Example.

We will use a library created by @Ganfra called MaterialSpinner.

This is differnt from Jared Rumler's MaterialSpinner.

MaterialSpinner(Ganfra's) will provides us a Spinner with the Material style. You can use it like any regular Spinner.

It allows you to:

  1. Add floating label text
  2. Add hint and
  3. Add error messages.

Top Android MaterialSpinner Examples in 2018

In this top-list tutorial, we explore some of the top material spinner libraries that you can easily use in your project. These are open source libraries have several advantages that we will explore in a short while.

Best Regards, Oclemy.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post