Tutorials Android Creating Drop Down List Using Android Spinner

Creating Drop Down List Using Android Spinner

This tutorial explains creating spinner in android and attaching event to spinner in android. Here in this tutorial, you’ll create a Simple spinner widget that displays a list of countries and shows appropriate flag as per selected country from the spinner.

1. Create a new new project. In this example we named it as SpinnerExample.

Declare Your Activity Layout (layout_main.xml)

Add a new layout file in \layout\layout_main.xml and insert the following code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dip" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/country_label"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/country_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/country_label" />

    <ImageView
        android:id="@+id/country_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical"
        android:scaleType="centerInside"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

In the above example, we have declared TextView, Spinner and a ImageView. The text behaves as a title for the spinner. When you select the Spinner, the ImageView to display the flag for selected country in Spinner.

3. Declaring String Arrays inside strings.xml

Below are the strings used in the application. Declare all of them in strings.xml file

<string-array name="countries_list">
        <item>Afghanistan</item>
        <item>Albania</item>
        <item>Australia</item>
        <item>Bangladesh</item>
        <item>Bhutan</item>
        <item>England</item>
        <item>Finland</item>
        <item>India</item>
        <item>Saudi Arabia</item>
        <item>Nepal</item>
    </string-array>

    <integer-array name="countries_flag_list">
        <item>@drawable/afghanistan</item>
        <item>@drawable/albania</item>
        <item>@drawable/australia</item>
        <item>@drawable/bangladesh</item>
        <item>@drawable/bhutan</item>
        <item>@drawable/england</item>
        <item>@drawable/finland</item>
        <item>@drawable/india</item>
        <item>@drawable/saudi_arabia</item>
        <item>@drawable/nepal</item>
    </integer-array>

4. Activity Java Class (MainActivity.java)

Now create a java class and name it as MainActivity.java. And paste the following code.

package com.example.spinnerexample;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

public class MainActivity extends Activity {

	private ImageView image;
	private String[] states;
	private Spinner spinner;
	private TypedArray imgs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		states = getResources().getStringArray(R.array.countries_list);
		imgs = getResources().obtainTypedArray(R.array.countries_flag_list);

		image = (ImageView) findViewById(R.id.country_image);
		spinner = (Spinner) findViewById(R.id.country_spinner);

		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, states);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(dataAdapter);

		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				image.setImageResource(imgs.getResourceId(
						spinner.getSelectedItemPosition(), -1));
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}
}

First we are initializing Spinner object by getting reference from the xml layout file using findViewById() method. Then creates a new ArrayAdapter, which binds each item in the string array to the initial appearance for the Spinner (which is how each item will appear in the spinner when selected). setOnItemSelectedListener() callback is registered to Spinner to handle the spinner event.

5. Output

Android Spinner Example

Total
0
Shares
guest
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Eric
Eric
8 years ago

where are you putting the bitmaps (or .jpg’s) for the flags?

javatechig
javatechig
8 years ago
Reply to  Eric

put all of them in your res/drawable folder

Ayeman Animations
Ayeman Animations
5 years ago
Reply to  Eric

Error:(19, 25) String types not allowed (at ‘prompt’ with value ‘country_label’). Can u help me with this error

pallavi
pallavi
6 years ago

how do I add a scroll bar to this spinner?

rakesh
rakesh
6 years ago

how do i set height of the spinner dropdown?

Ayeman Animations
Ayeman Animations
5 years ago

Error:(19, 25) String types not allowed (at ‘prompt’ with value ‘country_label’).. why do i get this error

Previous Post
Android Tutorials Javatechig

Websites Providing Attractive Android Tutorials for Developers

Next Post
Android Design Patterns

Mobile application design study

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
6
0
Would love your thoughts, please comment.x
()
x