Introduction
Creating a spinner
- <Spinner
- android:id="@+id/planets_spinner"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
To place the spinner or make a choice more available to the user we must specifiy a spinnerAdapter in the activity or fragments source code.
Populate the spinner with user choice
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">MyAndroidApp</string>
- <string name="country_prompt">Choose a country</string>
- <string-array name="country_arrays">
- <item>Malaysia</item>
- <item>United States</item>
- <item>Indonesia</item>
- <item>France</item>
- <item>Italy</item>
- <item>Singapore</item>
- <item>New Zealand</item>
- <item>India</item>
- </string-array>
- </resources>

Spinner Dropdown list
- <?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" >
- <Spinner
- android:id="@+id/spinner1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:entries="@array/country_arrays"
- android:prompt="@string/country_prompt" />
- <Spinner
- android:id="@+id/spinner2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/btnSubmit"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Submit" />
- </LinearLayout>

- package com.First.android;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.Spinner;
- import android.widget.Toast;
- public class MyAndroidAppActivity extends Activity {
- private Spinner spinner1, spinner2;
- private Button btnSubmit;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- addItemsOnSpinner2();
- addListenerOnButton();
- addListenerOnSpinnerItemSelection();
- }
- // add items into spinner dynamically
- public void addItemsOnSpinner2() {
- spinner2 = (Spinner) findViewById(R.id.spinner2);
- List<String> list = new ArrayList<String>();
- list.add("list 1");
- list.add("list 2");
- list.add("list 3");
- ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_spinner_item, list);
- dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spinner2.setAdapter(dataAdapter);
- }
- public void addListenerOnSpinnerItemSelection() {
- spinner1 = (Spinner) findViewById(R.id.spinner1);
- spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
- }
- // get the selected dropdown list value
- public void addListenerOnButton() {
- spinner1 = (Spinner) findViewById(R.id.spinner1);
- spinner2 = (Spinner) findViewById(R.id.spinner2);
- btnSubmit = (Button) findViewById(R.id.btnSubmit);
- btnSubmit.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MyAndroidAppActivity.this,
- "OnClickListener : " +
- "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
- "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
Now we have completed the Java source code and in this activity we just created a list references and a button that is the submit button and a drop down list in this activity. Now another activity is required to show the data selected from the Dropdown list.
- package com.First.android;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.Toast;
- public class CustomOnItemSelectedListener implements OnItemSelectedListener {
- public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
- Toast.makeText(parent.getContext(),
- "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
- Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- }
Some Key Methods
Let us analyze some key methods and XML attributes as given below. Using this table we might be able to understand the XML attributes.
This article illustrates the basics of spinners, a part of Input controls as we have learned in the previous article. The previous article was all about Input controls. Now we have made a drop-down list using spinners and some XML attributes as shown above.

Figure: XML attributes with key methods and their description

Gowtham RajamanickamPosted Apr 21, 2015, 2:49 AM
great