Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • Introduction of Spinner in Android

Introduction of Spinner in Android

 July 13  | 0 Comments

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

activity_main.xml

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

Create some code in onItemSelected() {..}

@Override

public void onItemSelected(AdapterView<?> parent, View arg1, int pos,

long arg3) {

parent.getItemAtPosition(pos);

if (pos == 0) {

 

 

 

 

 

 

android.R.layout.simple_spinner_item);

spinnerCity.setAdapter(adapter);

} else if (pos == 1) {

 

 

 

 

.createFromResource(this, R.array.city_pakisthan,

android.R.layout.simple_spinner_item);

spinnerCity.setAdapter(adapter);

} else if (pos == 2) {

 

 

 

 

.createFromResource(this, R.array.city_srilanka,

android.R.layout.simple_spinner_item);

spinnerCity.setAdapter(adapter);

}

}

 

OUTPUT :

 

IMAGE

IMAGE

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

About the Author

Hitesh Patel, currently working at AcadGild has two years of solid professional experience in designing and developing Android applications to his credit. He is an expert in Android app development using Eclipse IDE, Android Studio, Java, Web Services (JSON, XML), SQLite, Android SDK, and ADT plug-in.

He is proficient in common Android framework APIs (Telephony, Location, Maps, GCM) and is passionate about explaining it to others. He is an active blogger and has several projects developed by him in his bag.

Feel free to contact him at hitesh@acadgild.com in case you have any query.

>