Tek Eye Logo

Tek Eye

Load Values into an Android Spinner

This Android Spinner example takes a look at loading string items into the Spinner. The demo code provided is an Android Studio Spinner example project. The Spinner View is useful when you want the user to pick an item from a predetermined list, but do not want to take up a lot of screen space (such as using several radio buttons). Programmers moving to Android from other environments will notice a difference in terminology. The Android Spinner behaves in a similar fashion to what some may call a drop down list. A Spinner on other platforms is closer to the Android Pickers, which are often seen when setting dates and times on an Android device.

Android Dialog Spinner Demo

(This Android loading Spinner tutorial assumes that Android Studio is installed, a basic App can be created and run, and the code in this article can be correctly copied into Android Studio. The example code can be changed to meet your own requirements. When entering code in Studio add import statements when prompted by pressing Alt-Enter.)

Android Logo

The Android UI Pattern

Programmers coding with the Android SDK soon come across a familiar pattern when designing the user interface. There are the Views that make up the screens (managed by Activites). There is data that needs to be displayed in those Views. Finally there is the code that links the Views to the data. For some Views and types of data the code that links them together is provided by an Adapter. In this example the data is an array of strings, the View is the Spinner, and an ArrayAdapter is the link between the two.

Create a New Studio Project

Create a new project in Android Studio, here called Spinner Demo. An Empty Activity is used with other settings left at their default values.

Add the Data

The array of strings is defined in a values file in the res/values folder. Use the Project explorer to open the file strings.xml. Enter the values for the Spinner into a string array. Here a list of types of coffee is going to be used. Here is an example strings.xml with a string array called coffeeType:

    <string name="app_name">Spinner Demo</string>
    <string-array name="coffeeType">
        <item>Skinny Latte</item>
        <item>Espresso Corretto</item>
    <string name="coffeePrompt">Choose Coffee</string>

Add the Spinner

The Spinner is added to the activity_main.xml layout file (in the res/layout folder). Open the layout file and delete the default Hello World! TextView. From the Palette drag and drop and Spinner onto the layout. Update: You won't find the Spinner in some versions of Studio's Palette, use the Text tab to switch to XML mode, copy and paste the <Spinner.../> code below. Set Spinner id to chooseCoffee (if dropping onto a ConstraintLayout), also set the required constraints layout_width and layout_height to wrap_content. The activity_main.xml file will be similar to this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

        app:layout_constraintTop_toTopOf="parent" />

Add the Code to Load the Spinner

The Spinner added to the layout file is the basic framework, a layout will also be required to hold the data values in the collapsed and expanded state. Fortunately, for simple uses, Android includes default layouts. To connect the array to the Spinner an ArrayAdapter is created. The ArrayAdapter class has a static method that can take existing suitable resources and use them to create an ArrayAdapter instance. The method createFromResource() takes the current Context, the resource id of the string array, and the resource id of a layout. That layout will be used to display the selected array item when the Spinner is collapased (by default this layout is repeated to show the list of items in the expanded state). A layout for the data item has not been defined, instead an Android default simple_spinner_item layout is used. Here is the code for the MainActivity Java class:

package com.example.spinnerdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        //an adapter is linked to array and a layout
        ArrayAdapter adapter = ArrayAdapter.createFromResource(
        //link the adapter to the spinner
        Spinner coffeeChoice = (Spinner) findViewById(R.id.chooseCoffee);

Run the project to see the Spinner in action.

Android Spinner Demo

The Spinner supports a dialog style (see the first graphic at the top of the tutorial). To see it first set the Spinner prompt property to the Choose Coffee string (@string/coffeePrompt, see strings.xml above). Then change the spinnerMode property to dialog.

Spinner Attributes

The loading Spinner source code is available in loading_spinner.zip or from the Android Example Projects page.

See Also

Archived Comments

Isuru on March 8, 2013 at 5:38 am said: Thanks loads. It helps alot.

Pawan on May 6, 2013 at 12:56 pm said: Very Nice tutorial.

Author:  Published:  Updated:  

ShareSubmit to TwitterSubmit to FacebookSubmit to LinkedInSubmit to redditPrint Page

Do you have a question or comment about this article?

(Alternatively, use the email address at the bottom of the web page.)

 This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

markdown CMS Small Logo Icon ↓markdown↓ CMS is fast and simple. Build websites quickly and publish easily. For beginner to expert.

Articles on:

Android Programming and Android Practice Projects, HTML, VPS, Computing, IT, Computer History, ↓markdown↓ CMS, C# Programming, Using Windows for Programming

Free Android Projects and Samples:

Android Examples, Android List Examples, Android UI Examples

Tek Eye Published Projects