Android Spinners Dropdownlist Example Tutorial

Android Spinners Dropdownlist Example Tutorial

In Android it’s extremely simple to generate Drop Down Lists with the use of Spinner feature. In this tutorial, you will learn how to set up and show a Spinner.

We are going to learn how to create a Spinner Dropdownlist in our android application by using string-array resource in xml form.

Note that the same method can also be done through the use of Adapter that will populate the Spinner in run-time.

Before we start, the first thing I will do is to list the environment and tools I used in this android code snippet but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidSpinnersDropdownlist

Package: com.inducesmile.androidspinnersdropdownlist

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, make sure you change the package name if you did not use the same package.

Incorporating resources

We are going to create our string-array resource. Open the strings.xml file located in the following path res/values/strings.xml

When you open the strings.xml file, you are free to use the code or design view for your design.

The design view is simple tool you can use to add a lot of resources to your application such as strings, integers, and color values and so on.

But we are going to make use of the standard way which is editing the strings.xml file by hand. In the bottom of the screen, hit the string.xml tab.

Copy and paste the following code in your strings.xml file.


<string name="app_name">AndroidSpinnersDropdownlist</string>

<string name="hello_world">Hello world!</string>

<string name="action_settings">Settings</string>

<string name="select">Choose an object</string>

<string-array name="object_array">










Thus we’ve merely generated a few string resources that we can make use of in a lot of ways and in a lot of places in our app. We’ve as well produced a string-array resource. This is a very significant feature when you want to group together a collection of strings that you wish to make use of as a list in your application.

In this instance, we will load one of the spinners with this specific array of strings, and it will routinely create the particular options on the drop down list.

One of the major advantage of using this method is that offers the freedom to change the constitute of the string array without affecting the code.

Spinner User Interface

Open res/layout/activity_main.xml file:

Copy and paste the following code:

<RelativeLayout xmlns:android=""







android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">









android:layout_marginTop="30dp" />


In the code above, observe the android:entries=”@array/object_array” attribute. This tells the Spinner to make use of a specific array resource (in this instance the string-array we defined earlier) in order to produce the options on the drop down list.

The spinner attribute called prompt is use to create the header information for the spinner. This is important because it let users to understanding what it does.

Copy and paste the following code in file


import android.os.Bundle;


import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.Toast;

public class MainActivity extends ActionBarActivity {


protected void onCreate(Bundle savedInstanceState) {



Spinner spinner = (Spinner) findViewById(;

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



spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {


public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Toast.makeText(getApplicationContext(), "Item number: " + position, Toast.LENGTH_LONG).show();





public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(, menu);

return true;



public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == {

return true;


return super.onOptionsItemSelected(item);



Save the file and run your project. If everything works for you, an image like this will appear on your test device.

Android Spinners Dropdownlist Example Tutorial

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published


No Responses

Add a Comment