Introduction

This article explains how to create a Spinner in Android. A Spinner is similar to a drop-down list. It provides a quick way to select a value from a list. It displays the currently selected value and touching the spinner gives a drop-down list from which we can choose the required value.
Step 1
"strings.xml" used in this project is:
  1. <resources>
  2. <string name="app_name">Spinner</string>
  3. <string name="action_settings">Settings</string>
  4. <string name="hello_world">Hello world!</string>
  5. <string name="choose_chocolate">Choose Chocolate</string>
  6. <string-array name="chocolate">
  7. <item>Crackles</item>
  8. <item>Fruit and Nut</item>
  9. <item>Dairy milk silk</item>
  10. </string-array>
  11. </resources>
Note that there are two ways to add items to a Spinner, one through the string array and the other programmatically. We will be using both techniques. The Chocolate Spinner will get its list as a string array and Ice Cream Spinner will get it's list programmatically.
Step 2
Open the "activity_main" layout file and add the following code to it:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. android:paddingBottom="@dimen/activity_vertical_margin"
  10. tools:context=".MainActivity"
  11. android:orientation="vertical">
  12. <RelativeLayout
  13. android:layout_height="wrap_content"
  14. android:layout_width="fill_parent">
  15. <TextView
  16. android:text="Chocolate"
  17. android:id="@+id/txtChoco"
  18. android:layout_height="wrap_content"
  19. android:layout_width="wrap_content"
  20. android:layout_marginTop="20dp"
  21. android:textSize="30dp"/>
  22. <Spinner
  23. android:id="@+id/chocolate_spinner"
  24. android:layout_height="wrap_content"
  25. android:layout_width="fill_parent"
  26. android:entries="@array/chocolate"
  27. android:prompt="@string/choose_chocolate"
  28. android:layout_toRightOf="@+id/txtChoco"
  29. android:layout_marginLeft="10dp"
  30. android:layout_marginTop="15dp"/>
  31. </RelativeLayout>
  32. <RelativeLayout
  33. android:layout_height="wrap_content"
  34. android:layout_width="fill_parent"
  35. android:layout_marginTop="30dp">
  36. <TextView
  37. android:text="Ice Cream"
  38. android:id="@+id/txtIce"
  39. android:layout_height="wrap_content"
  40. android:layout_width="wrap_content"
  41. android:layout_marginTop="20dp"
  42. android:textSize="30dp"/>
  43. <Spinner
  44. android:id="@+id/ice_cream_spinner"
  45. android:layout_height="wrap_content"
  46. android:layout_width="fill_parent"
  47. android:layout_toRightOf="@+id/txtIce"
  48. android:layout_marginLeft="10dp"
  49. android:layout_marginTop="15dp"
  50. />
  51. </RelativeLayout>
  52. <Button
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:text="Done"
  56. android:layout_marginLeft="130dp"
  57. android:layout_marginTop="100dp"
  58. android:id="@+id/done"/>
  59. </LinearLayout>
Note that a Spinner is added to create a Spinner in the layout. We have two Spinners here, one for chocolates and the other for Ice Creams.
The layout looks like:
im1.jpg
Step 3
Create a new layout file.
Right-click on layout then select "New" -> "Layout Resource File". Name it "selected_layout".
This file layout shows the selected values of spinners in a button click.
Add the following code to it:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#ffd088" >
  6. <TextView android:id="@+id/display_choco"
  7. android:layout_height="wrap_content"
  8. android:layout_width="fill_parent"
  9. android:textSize="30dp"
  10. android:layout_marginTop="30dp"
  11. android:layout_marginLeft="30dp"/>
  12. <TextView android:id="@+id/display_ice"
  13. android:layout_height="wrap_content"
  14. android:layout_width="fill_parent"
  15. android:textSize="30dp"
  16. android:layout_marginTop="30dp"
  17. android:layout_marginLeft="30dp"/>
  18. </LinearLayout>
Step 4
Open "MainActivity" and add the following code to it:
  1. package com.chhavi.spinner;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.Button;
  9. import android.widget.Spinner;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. public class MainActivity extends Activity {
  15. private Spinner choco, ice;
  16. private Button done;
  17. TextView choco_display, ice_display;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. addIceCream();
  23. choco = (Spinner) findViewById(R.id.chocolate_spinner);
  24. choco.setOnItemSelectedListener(new SelectingItem());
  25. done=(Button)findViewById(R.id.done);
  26. ice=(Spinner)findViewById(R.id.ice_cream_spinner);
  27. done.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. setContentView(R.layout.selected_layout);
  31. choco_display=(TextView)findViewById(R.id.display_choco);
  32. ice_display=(TextView)findViewById(R.id.display_ice);
  33. choco_display.setText("Chocolate: "+String.valueOf(choco.getSelectedItem()));
  34. ice_display.setText("Ice cream: "+String.valueOf(ice.getSelectedItem()));
  35. }
  36. });
  37. }
  38. public void addIceCream()
  39. {
  40. ice = (Spinner) findViewById(R.id.ice_cream_spinner);
  41. List<String> list = new ArrayList<String>();
  42. list.add("Vanilla");
  43. list.add("Fruit and Nut");
  44. list.add("Choco chips");
  45. ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
  46. dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  47. ice.setAdapter(dataAdapter);
  48. }
  49. @Override
  50. public boolean onCreateOptionsMenu(Menu menu) {
  51. // Inflate the menu; this adds items to the action bar if it is present.
  52. getMenuInflater().inflate(R.menu.main, menu);
  53. return true;
  54. }
  55. }
In the code above, the "addIceCream" method adds a list of items to the Spinner for Ice Cream. Note the two ways of adding values to the Spinner. The SpinnerObject.getSelectedItem method returns the item currently selected.
Step 5
Create a new Java class in the same package. Name it "SelectingItem" and add the following code to it:
  1. package com.chhavi.spinner;
  2. import android.view.View;
  3. import android.widget.AdapterView;
  4. import android.widget.Toast;
  5. public class SelectingItem implements AdapterView.OnItemSelectedListener {
  6. public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
  7. Toast.makeText(parent.getContext(),
  8. "Selecting Item : " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
  9. }
  10. @Override
  11. public void onNothingSelected(AdapterView<?> arg0) {
  12. // TODO Auto-generated method stub
  13. }
  14. }
Output snapshots:
im2.jpg
Clicking on the first spinner we will get it's drop-down list as:
im3.jpg
Selecting one item from the list above:
im4.jpg
Similarly, selecting a value from the second list and clicking on done gives:
im5.jpg
Thank you