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

Before proceedihg, 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;  
</code>

And your class must implement AdapterView.OnItemSelectedListner.

<code>
  1. And, your class must implements AdapterView.OnItemSelectedListner  
</code>

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

<code>

  1. public class MainActivity extends ActionBarActivity implements    
  2. AdapterView.OnItemSelectedListener {  
  3. ….  
  4. }  

 </code>

Next, we need an array of strings that we will use in furthur sections.

<code>
  1. String[] iplTeam= {"KKR""CSK""RR""KXIP""RR""MI" };  
</code>

And now we will handle our Views that we created in the XML file.

So, move on to the onCreate() merthod and add:

<code>
  1. // Linking the Views to Java’s instances  
  2. Spinner spin1=(Spinner) findViewById(R.id.spinner1);  
  3. spin1.setOnItemSelectedListener((OnItemSelectedListener) this);  
</code>

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);  
 </code>

At last, link the adapter to the spinner instance.

<code>

  1. // Assigning the adapter to Spinner  
  2. spin1.setAdapter(adapter);  

 </code>

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.

<code>

  1. Toast.makeText(getApplicationContext(), "You have Chosen :"+iplTeam[arg2], Toast.LENGTH_LONG).show();  

 </code>

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.

Up Next
    Ebook Download
    View all
    Learn
    View all