Basics of Input Control in Android : Part 2 Spinners

Introduction

Spinners may be defined as the tools of programs that enable users to select from a set of items or any other dialog from the list mainly in an application. It is a quick way to select and touch a phone that makes it very impressive and swift. It is a type of drop down list touching the spinner that displays a dropdown menu with all other available values and from that list the user can select only one.

It is a part of Input Controls and a subclass of widgets. Extending the widgets class in our spinner class is a very simple way to create a spinner. To create a spinner type object just add an attribute in the layout file in the res directory.

Creating a spinner

Just create an attribute specifying a Spinner object in the XML file and try to apply the following procedure and the code as well just for illustration. 

  1. <Spinner  
  2.    android:id="@+id/planets_spinner"  
  3.    android:layout_width="fill_parent"  
  4.    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

The choice is providing using the a SpinnerAdapter such as an ArrayAdapter class and if choices are available in a database then its object will do something. Let us see how we are doing that.

String.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4. <string name="app_name">MyAndroidApp</string>  
  5. <string name="country_prompt">Choose a country</string>  
  6.   
  7. <string-array name="country_arrays">  
  8.     <item>Malaysia</item>  
  9.     <item>United States</item>  
  10.     <item>Indonesia</item>  
  11.     <item>France</item>  
  12.     <item>Italy</item>  
  13.     <item>Singapore</item>  
  14.     <item>New Zealand</item>  
  15.     <item>India</item>  
  16.    </string-array>  
  17.   
  18. </resources>  
This code might produce the following output as a design, in other words it contains a list from which the user will choose.

country dropdown  

Spinner Dropdown list

The preceding code is just for adding resources to the String.xml file. Now to add attributes to the layout.xml file (res/layout/main.xml).

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:layout_width="fill_parent"  
  4. android:layout_height="fill_parent"  
  5. android:orientation="vertical" >  
  6.   
  7. <Spinner  
  8.    android:id="@+id/spinner1"  
  9.    android:layout_width="match_parent"  
  10.    android:layout_height="wrap_content"  
  11.    android:entries="@array/country_arrays"  
  12.    android:prompt="@string/country_prompt" />  
  13.   
  14. <Spinner  
  15.    android:id="@+id/spinner2"  
  16.    android:layout_width="match_parent"  
  17.    android:layout_height="wrap_content" />  
  18.   
  19. <Button  
  20.    android:id="@+id/btnSubmit"  
  21.    android:layout_width="wrap_content"  
  22.    android:layout_height="wrap_content"  
  23.    android:text="Submit" />  
  24.   
  25. </LinearLayout>  
The XML design of the preceding code produces  the following output as only the design. 
design

Java code MainActivity.java

Corresponding to the layout file we must write the Java code and add the references to the Java file and complete the code using the ArrayAdapter class. We are defining the attributes and relevant references.
  1. package com.First.android;  
  2.    
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.ArrayAdapter;  
  10. import android.widget.Button;  
  11. import android.widget.Spinner;  
  12. import android.widget.Toast;  
  13.    
  14. public class MyAndroidAppActivity extends Activity {  
  15.    
  16.   private Spinner spinner1, spinner2;  
  17.   private Button btnSubmit;  
  18.    
  19.   @Override  
  20.   public void onCreate(Bundle savedInstanceState) {  
  21.     super.onCreate(savedInstanceState);  
  22.     setContentView(R.layout.main);  
  23.    
  24.     addItemsOnSpinner2();  
  25.     addListenerOnButton();  
  26.     addListenerOnSpinnerItemSelection();  
  27.   }  
  28.    
  29.   // add items into spinner dynamically  
  30.   public void addItemsOnSpinner2() {  
  31.    
  32.     spinner2 = (Spinner) findViewById(R.id.spinner2);  
  33.     List<String> list = new ArrayList<String>();  
  34.     list.add("list 1");  
  35.     list.add("list 2");  
  36.     list.add("list 3");  
  37.     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,  
  38.         android.R.layout.simple_spinner_item, list);  
  39.     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  40.     spinner2.setAdapter(dataAdapter);  
  41.   }  
  42.    
  43.   public void addListenerOnSpinnerItemSelection() {  
  44.     spinner1 = (Spinner) findViewById(R.id.spinner1);  
  45.     spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());  
  46.   }  
  47.    
  48.   // get the selected dropdown list value  
  49.   public void addListenerOnButton() {  
  50.    
  51.     spinner1 = (Spinner) findViewById(R.id.spinner1);  
  52.     spinner2 = (Spinner) findViewById(R.id.spinner2);  
  53.     btnSubmit = (Button) findViewById(R.id.btnSubmit);  
  54.    
  55.     btnSubmit.setOnClickListener(new OnClickListener() {  
  56.    
  57.       @Override  
  58.       public void onClick(View v) {  
  59.    
  60.         Toast.makeText(MyAndroidAppActivity.this,  
  61.         "OnClickListener : " +   
  62.                 "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +   
  63.                 "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),  
  64.             Toast.LENGTH_SHORT).show();  
  65.       }  
  66.    
  67.     });  
  68.   }  
  69. }  

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.

  1. package com.First.android;  
  2.   
  3. import android.view.View;  
  4. import android.widget.AdapterView;  
  5. import android.widget.AdapterView.OnItemSelectedListener;  
  6. import android.widget.Toast;  
  7.   
  8. public class CustomOnItemSelectedListener implements OnItemSelectedListener {  
  9.   
  10.    public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {  
  11.       Toast.makeText(parent.getContext(),   
  12.       "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),  
  13.       Toast.LENGTH_SHORT).show();  
  14.    }  
  15.   
  16. @Override  
  17.    public void onNothingSelected(AdapterView<?> arg0) {  
  18.    // TODO Auto-generated method stub  
  19.    }  
  20.   
  21. }  

 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. 

xml attributes image

Figure: XML attributes with key methods and their description.

Summary

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.

Up Next
    Ebook Download
    View all
    Learn
    View all