How to Create Android Spinner (Drop Down List) using string-array Resource in xml Form

In android we can create drop down list of choices using spinner. There are two ways to populate spinner with options. The first way is by creating a hard coded string-array resource in xml form. The second way is to create the options in the drop down list dynamically in run time using an Adapter.

I am going to demonstrate the first way(more general purpose) here.

Key XML attributes
android:id is to be used for Java reference to assign an event handler.
android:entries is an XML entry defining an array of choices. Can be in strings.xml or a separate file (e.g., arrays.xml).

<string-array name="some_name">
<item>choice 1</item>
<item>choice 2</item>
…
</string-array>

XML: Layout File Entry
res/layout/activity_spinner_ex1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    
    <!-- Text Label -->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/lblCountry" />

     <!-- Spinner Element -->
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:entries="@array/country_arr"/>

</LinearLayout>

XML: Strings File Entries
res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Spinner Example</string>
    <string name="action_settings">Settings</string>
    <string name="lblCountry">Select Country</string>

    <string-array name="country_arr">
        <item>Afghanistan</item>
        <item>Bangladesh</item>
        <item>Bhutan</item>
        <item>India</item>
        <item>Maldives</item>
        <item>Sri Lanka</item>
        <item>Nepal</item>
        <item>Pakistan</item>
    </string-array>
</resources>

Main Activity
SpinnerEx1Activity.java

package com.bar.example.androidspinnerexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Spinner;

public class SpinnerEx1Activity extends Activity {
Spinner sprCoun;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_spinner_ex1);
		sprCoun = (Spinner)findViewById(R.id.spinner1);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.spinner_ex1, menu);
		return true;
	}

}

Emulator Screenshot:

android_spinner_ex1

You can download all android example from here.

 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

1,678` Total Views

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

*


eight × 3 =