Introduction:
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.
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
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 |
<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
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 |
<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() {..} :
1 2 3 4 5 |
SpinnerCountry = (Spinner) findViewById(R.id.spinnerCountry); spinnerCity = (Spinner) findViewById(R.id.spinnerCity); spinnerCountry.setOnItemSelectedListener(this); |
1 2 3 |
spinnerCity = (Spinner) findViewById(R.id.spinnerCity); spinnerCountry.setOnItemSelectedListener(this); |
Create some code in onItemSelected() {..}
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 |
@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 :
Conclusion :
You can customize the spinner as per your requirements. But the basic use of spinner is selecting the single item from the list of items. So, Spinners are very important for any android application. You can see the real time applications like Gmail, Google Docs, Music apps and many more where spinner 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 support@acadgild.com
Leave a Reply