Android Spinner


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
Spinner{}

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.
<Spinner
    android:id="@+id/mySpinner"
    android:layout_width="fill_parent"
    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(R.id.mySpinner);

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"?>
<resources>
    <string-array name="galaxies_array">
        <item>Milky Way</item>
        <item>Andromeda</item>
        <item>Whirlpool</item>
        <item>Sombrero</item>
        <item>Cartwheel</item>
        <item>StarBust</item>
        <item>Pinwheel</item>
        <item>Leo</item>
    </string-array>
</resources>

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

After that:

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

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:

spinner.setAdapter(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 in our spinner. Our programming language is Kotlin.

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.

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. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post