How to Set Spinner in Android

Spinner

 
Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value.
 
Procedure
  1. Start the Eclipse IDE.
  2. Create a new project.
  3. Create a MainActivity.java file.
  4. Create an activity_main.xml for layout design.
  5. Declare a spinner in the XML layout, for example:
    1. <Spinner  
    2. android:layout_width="wrap_content"  
    3. android:layout_height="wrap_content"  
    4. android:id="@+id/ss"/> 
       
  6. Declare a string array with some elements and pass these elements to the ArrayAdapter.
     
    For instance
    1. String[] str={"select","yellow","red","Green","purple","blue"};  
    2. ArrayAdapter<String> adap=new ArrayAdapter<String>  
    3. (this, android.R.layout.simple_spinner_item,str);  
       
The code is given below.
 
MainActivity.java
  1. package com.example.spinnertodayy;  
  2. import android.app.Activity;  
  3. import android.graphics.Color;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.AdapterView;  
  7. import android.widget.AdapterView.OnItemSelectedListener;  
  8. import android.widget.ArrayAdapter;  
  9. import android.widget.RelativeLayout;  
  10. import android.widget.Spinner;  
  11. import android.widget.TextView;  
  12. import android.widget.Toast;  
  13. public class MainActivity extends Activity {  
  14. Spinner s1;  
  15. RelativeLayout ll;  
  16. String[] str={"select","yellow","red","Green","purple","blue"};  
  17. @Override  
  18. protected void onCreate(Bundle savedInstanceState) {  
  19. super.onCreate(savedInstanceState);  
  20. setContentView(R.layout.activity_main);  
  21. s1=(Spinner) findViewById(R.id.ss);  
  22. ll=(RelativeLayout) findViewById(R.id.rr);  
  23. ArrayAdapter<String> adap=new ArrayAdapter<String>  
  24. (this, android.R.layout.simple_spinner_item,str);  
  25. s1.setAdapter(adap);  
  26. s1.setOnItemSelectedListener(new OnItemSelectedListener() {  
  27. public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
  28. long arg3) {  
  29. // TODO Auto-generated method stub  
  30. String ss1=arg0.getItemAtPosition(arg2).toString();  
  31. String ss=((TextView)arg1).getText().toString();  
  32. Toast.makeText(getApplicationContext(), ss, 10000).show();  
  33. }  
  34. public void onNothingSelected(AdapterView<?> arg0) {  
  35. // TODO Auto-generated method stub  
  36. }  
  37. });  
  38. }  
  39.  
activity_main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.    xmlns:tools="http://schemas.android.com/tools"  
  3.    android:layout_width="match_parent"  
  4.    android:layout_height="match_parent"  
  5.    android:id="@+id/rr"  
  6.    android:paddingBottom="@dimen/activity_vertical_margin"  
  7.    android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.    android:paddingRight="@dimen/activity_horizontal_margin"  
  9.    android:paddingTop="@dimen/activity_vertical_margin"  
  10.    tools:context=".MainActivity" >  
  11.    <Spinner  
  12.       android:layout_width="wrap_content"  
  13.       android:layout_height="wrap_content"  
  14.       android:id="@+id/ss"/>  
  15. </RelativeLayout> 
     
Output