How To Work With Android Spinner Widget

Today I’m going to show how to work with android spinner widget. the spinner widget is same as drop-down list use to selecting items.But it appears differently.following example shows you the way using it. so lets start the example.

  1. First we going to create example project.then create simple layout.to do that drag and drop Spinner widget and EditText text field to your main layout.your main.xml shows like following.
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <?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" android:padding="10dp">
        <Spinner
            android:id="@+id/spinnerWeekDays"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:prompt="@string/weekday_prompt"/>
        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text" android:layout_marginTop="20dp">
            <requestFocus />
     
        </EditText>
    </LinearLayout>
    firstLayout
  2. Next step is we want define items that going to display in spinner.So add following xml code to your strings.xml file.
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <!-- weekday_prompt string is the string that initially show in spinner  -->  
        <string name="weekday_prompt">Choose a Week Day</string>
        <!-- weekday_array is the item list we going to show for selection in spinner -->
        <string-array name="weekday_array">
            <item>Saturday</item>
            <item>Sunday</item>
            <item>Monday</item>
            <item>Tuesday</item>
            <item>Wednesday</item>
            <item>Thursday</item>
            <item>Friday</item>
        </string-array>
    here we have one string value that going to show initially in spinner and string array that going to show for selection in spinner.your final string.xml file look like following.
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, AndroidSpinnerExampleActivity!</string>
        <string name="app_name">AndroidSpinnerExample</string>
         
        <!-- weekday_prompt string is the string that initially show in spinner  -->  
        <string name="weekday_prompt">Choose a Week Day</string>
        <!-- weekday_array is the item list we going to show for selection in spinner -->
        <string-array name="weekday_array">
            <item>Saturday</item>
            <item>Sunday</item>
            <item>Monday</item>
            <item>Tuesday</item>
            <item>Wednesday</item>
            <item>Thursday</item>
            <item>Friday</item>
        </string-array>
         
    </resources>
    to show our initial text in spinner we should set android:prompt property value to weekday_prompt of our spinner widget in main.xml file.insert it as following code in spinner tag.
    ?
    1
    2
    3
    4
    5
    6
    <Spinner
            android:id="@+id/spinnerWeekDays"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:prompt="@string/weekday_prompt"/>
  3. Finally we finish our layout and resource part.So next we can implement functionality of our spinner widget.following AndroidSpinnerExampleActivity java file shows you how implement logic.commented all necessary steps.
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    package android.guide.spinner;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.Spinner;
     
    public class AndroidSpinnerExampleActivity extends Activity {
        /** Called when the activity is first created. */
     //define EditText
     EditText selectedWeekDay;
      
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            //get the EditText form resource
            selectedWeekDay = (EditText) findViewById(R.id.editText);
                  
            //get the spinner from resource
            Spinner weekDays = (Spinner) findViewById(R.id.spinnerWeekDays);
             
            //then defining ArrayAdapter using weekday_array
            ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this, R.array.weekday_array,
              android.R.layout.simple_spinner_item);
            //set the appearance of widget items that show when open the widget
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            //set the adapter to spinner
            weekDays.setAdapter(adapter);
            //set the action listener
            weekDays.setOnItemSelectedListener(listener);
             
        }
     // implement action listener type of OnItemSelectedListener
     private OnItemSelectedListener listener =new OnItemSelectedListener() {
      //do what ever you want to do when item selected
      public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
       //i get the item using selected item position and set it into selectedWeekDay
       selectedWeekDay.setText(parent.getItemAtPosition(pos).toString());  
      }
     
      public void onNothingSelected(AdapterView<?> parent) {
        
      }
     
     };
    }
  4. Following screen shots show you how our example works. 
spinner-01spinner-selectspinner-selected 
Download source code here AndroidSpinnerExample

FEEDBACKS ARE WARMLY WELCOME

Labels: , ,