All CategoriesAndroid App Development

Introduction of Spinner in Android

Introduction to Spinners:
Spinners are used for selecting one item from a given list, like drop down list. In the default state, a spinner shows its currently selected value.Touching the spinner displays a dropdown list with all available list of values, from which the user can select one value.You can have an application in which the entries of second spinner depends on the selected value in first spinner. 
Example : In First spinner, select a country, and you will get a city according to the country in second spinner what you have selected in first spinner.
Real time Example:
For example. When you are using Gmail application you would get drop down menu as shown below, you need to select an item from a drop down menu.
spinners
Methods & Classes:
Methods :
onItemSelected() {..}
onNothingSelected() {..}
getItemPosition() {..}
Class :
Spinner
Example with code:
In this sample application we will take two spinners. First one for selecting country and the other for selecting city based on the country that is selected.
There are some steps to follow:
1. Create a new application (File > New > Android Application Project).
2. After creation of project, go to res/values folder and click on strings.xml.
3. Add values for countries and cities.
Strings.xml

<string-array name="country_array">
<item>India</item>
<item>Pakisthan</item>
<item>Sri Lanka</item>
</string-array>
<string-array name="city_india">
<item>Mumbai</item>
<item>Chennai</item>
<item>Kolkata</item>
<item>Bangalore</item>
</string-array>
<string-array name="city_pakisthan">
<item>Karachi</item>
<item>Lahore</item>
<item>Faisalabad</item>
<item>Rawalpindi</item>
</string-array>
<string-array name="city_srilanka">
<item>Colombo</item>
<item>Dehiwala-Mount Lavinia</item>
<item>Moratuwa</item>
<item>Kotte</item>
</string-array>

Take two spinners and add a code in activity_main.xml.
activity_main.xml

<Spinner
android:id="@+id/spinnerCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:entries="@array/country_array" />
<Spinner
android:id="@+id/spinnerCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:entries="@array/city_india" />

MainActiivity.java
Initialize Spinner :
Spinner spinnerCountry, spinnerCity;
Add code in onCreate() {..} :

SpinnerCountry = (Spinner) findViewById(R.id.spinnerCountry);
spinnerCity = (Spinner) findViewById(R.id.spinnerCity);
spinnerCountry.setOnItemSelectedListener(this);
spinnerCity = (Spinner) findViewById(R.id.spinnerCity);
spinnerCountry.setOnItemSelectedListener(this);

Create some code in onItemSelected() {..}

@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
long arg3) {
parent.getItemAtPosition(pos);
if (pos == 0) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.city_india,
android.R.layout.simple_spinner_item);
spinnerCity.setAdapter(adapter);
} else if (pos == 1) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.city_pakisthan,
android.R.layout.simple_spinner_item);
spinnerCity.setAdapter(adapter);
} else if (pos == 2) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.city_srilanka,
android.R.layout.simple_spinner_item);
spinnerCity.setAdapter(adapter);
}
}

 
OUTPUT :
spinners
 
spinners
Conclusion :
You can customize the spinner as per your requirements. But the basic use of it is selecting the single item from the list of items. So, they are very important for any android application. You can see the real-time applications like Gmail, Google Docs, Music apps and many more where this is used. This is the basic idea about the android widgets, which is very popular in Android development.
Hope this blog was helpful. For any queries mail us at [email protected]
Keep visiting our site www.acadgild.com for more updates on Android and other technologies.

Android Programming

Tags

One Comment

  1. Nonetheless, I don’t know exactly how far you can obtain with a limited transmission capacity using GSA
    Search Engine Ranker.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Close