Day 7: Android Spinner or ComboBox


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 does the same job except the platform they work for.



  • 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:

  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,, where are adding a Spinner first, then adding an ArrayAdapter to it.

Before proceedihg, ensure you have all these packages in your project.

  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.

  1. And, your class must implements AdapterView.OnItemSelectedListner  

After implementing succesffully, two new functions will be genearted in down of your page,


  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.

  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() merthod and add:

  1. // Linking the Views to Java’s instances  
  2. Spinner spin1=(Spinner) findViewById(;  
  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.

  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.


  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 implemnted the class with AdapterView.


  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 

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.

Up Next
    Ebook Download
    View all
    View all