Day 7: Android Spinner or ComboBox

Introduction

 
You have often used combo boxes in your desktop application and many of times you have dragged a Combobox control when you were learning Windows Forms. Just like a combo box in the Toolbox, we have a Spinner in the Android Pallete.
 
Both do the same job except the platform they work for.
 
introduction
 
Procedures 
Step 1
Begin by making an Android project in your respective IDE with a working emulator.
Step 2
First, we add a Spinner from the Palette in the main_activity.xml and it generates something like this:
 
Code
  1. <Spinner    
  2.         android:id="@+id/spinner1"    
  3.         android:layout_width="match_parent"    
  4.         android:layout_height="wrap_content" />   
    Step 3
    We will now add our code to the Java source file, MainActivity.java, were are adding a Spinner first, then adding an ArrayAdapter to it.
     
    Before proceeding, ensure you have all these packages in your project.
     
    Code
    1. import android.view.View;    
    2. import android.widget.AdapterView;    
    3. import android.widget.AdapterView.OnItemSelectedListener;    
    4. import android.widget.ArrayAdapter;    
    5. import android.widget.Spinner;    
    6. import android.widget.Toast;   
      And your class must implement AdapterView.OnItemSelectedListner.
       
      Code
      1. And, your class must implement AdapterView.OnItemSelectedListner  
      After implementing successfully, two new functions will be generated in the down of your page,
       
      Code
      1. public class MainActivity extends ActionBarActivity implements      
      2. AdapterView.OnItemSelectedListener {    
      3. ….    
      4. }   
        Next, we need an array of strings that we will use in furthur sections.
         
        Code
        1. String[] iplTeam= {"KKR""CSK""RR""KXIP""RR""MI" };   
        And now we will handle our Views that we created in the XML file.
         
        So, move on to the onCreate() method and add:
         
        Code
        1. // Linking the Views to Java’s instances    
        2. Spinner spin1=(Spinner) findViewById(R.id.spinner1);    
        3. spin1.setOnItemSelectedListener((OnItemSelectedListener) this);   
        And, we will bind the string array to the Spinner itself. To do this, we need an ArrayAdapter. And, there we will define the kind of Spinner we want.
         
        Code
        1. // Now, Create a Array Adapter    
        2. ArrayAdapter adapter=new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, iplTeam);    
        3. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
        At last, link the adapter to the spinner instance.
         
        Code
        1. // Assigning the adapter to Spinner    
        2. spin1.setAdapter(adapter); 
        We have now configured the Spinner and Adapter. But, when we select the item from the Spinner then there must be something that shows the value of what we selected.
         
        And for that, we will use a Toast message that shows the selected value.
         
        Put the following code in the onSelectedItem() method that was generated when we implemented the class with AdapterView.
         
        Code
        1. Toast.makeText(getApplicationContext(), "You have Chosen :"+iplTeam[arg2], Toast.LENGTH_LONG).show();   
        Here, args2 is the selected index for the Spinner and args3 is the ID itself.
         
        If you have written your code successfully, then you may get this as output:
         
         Output 1        Output 2  
         
        Output 3
         

        Conclusion

         
        If you experience any problem then use Quick Fix (Ctrl+1) since it will solve most of your import problems or notation errors. Otherwise, go for the solution file that I have enclosed for this project.


        Similar Articles