---
title: "Spinner Control in Android"  
description: "In this article I am going to explain the Spinner control and how to add data as well as creating selector for selected Item."  
author: "Manoj Pandey"  
published: 2014-03-26  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1387/spinner-control-in-android  
category: "android"  
tags: ["android"]  
reading_time: 3 minutes  

---

# Spinner Control in Android

In this article I am going to explain the Spinner control and how to [add data](https://www.mindstick.com/forum/287/how-to-add-data-in-drop-down-list) as well as creating [selector](https://www.mindstick.com/forum/504/what-is-id-selector) for selected Item.\

The spinner [controls](https://www.mindstick.com/articles/66/how-to-create-controls-at-run-time-in-csharp-dot-net) is similar to the Combo Box in C#. It displays a list to select from in a popup window so it may become a [better choice](https://answers.mindstick.com/qa/97753/which-is-a-better-choice-netflix-or-hbo) over List View if you want to save space.

**Follow the steps which are given below and you can use this example for [learning](https://yourviews.mindstick.com/view/88279/my-journey-learning-content-writing-at-mindstick-training-from-experienced-content-writers) the [concepts](https://www.mindstick.com/articles/13127/macro-and-microeconomics-concepts-in-relation-to-global-organization-management) of Spinner Selector.**

- Open Eclipse
- Select New [Android Application](https://www.mindstick.com/articles/1653/creating-libraries-for-android-applications) [Project](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful)
- Enter Application Name as SpinnerSample
- Click Next
- Click Finish.

Now go to the **res/Layout/activity_main.xml** and add paste the following code

```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:focusableInTouchMode="false"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:scrollbarAlwaysDrawVerticalTrack="false"tools:context=".MainActivity" > xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:focusableInTouchMode="false" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:scrollbarAlwaysDrawVerticalTrack="false" tools:context=".MainActivity" >
```

Paste the following code in **SpinnerSample/src/ com.example.spinnersample/**

**MainActivity.java**

```
package com.example.spinnersample;
public class MainActivity extends Activity implements OnItemSelectedListener {
Spinner spin; String[] myArray = { "Select City . . ", "Peris", "Mumbai", "London", 
"Canada", "Delhi", "Rome", "Italy", "Lahore", "Washingaton",
"Chennai" };
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (Spinner) findViewById(R.id.spin_CityList);
spin.setOnItemSelectedListener(this);
//set the value in spinner control which is set by setadapter method.
spin.setAdapter(new MyAdapter(this,
android.R.layout.simple_spinner_dropdown_item, myArray));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
//Get the value of selected position
String sCity = parent.getItemAtPosition(pos).toString();
if (sCity == "Select City . . ") {
} else {
Toast.makeText(MainActivity.this, " This city is- : " + sCity,
Toast.LENGTH_LONG).show();
}}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
// This class is made for to find the items of spinner in textview which textview is created dynamically in the program and class is extends by Array adapter.
// This class should be extends by ArrayAdapter<String>.
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
LayoutInflater inflater = getLayoutInflater();
View spinnerItem = inflater.inflate(
android.R.layout.simple_spinner_item, null);
TextView mytext = (TextView) spinnerItem
.findViewById(android.R.id.text1);
mytext.setText(myArray[position]);
mytext.setTextColor(Color.RED);
mytext.setTextSize(18);
int selected = spin.getSelectedItemPosition();
if (position == selected) {
if (selected == 0) {
} else {
//Set the background color of selected item.
spinnerItem.setBackgroundColor(Color.GREEN);
}
}
return spinnerItem;
}
}
}
```

Now **Run the Android application**

Choose any one of city (as shown below).

![Spinner Control in Android](https://www.mindstick.com/mindstickarticle/c5e8b081-e855-413d-b8d6-0ff084193d5c/images/ec0bf64c-9209-498b-b78a-d8aaefeeb567.png)

After selection of any city like Canada then (as shown below)

![Spinner Control in Android](https://www.mindstick.com/mindstickarticle/c5e8b081-e855-413d-b8d6-0ff084193d5c/images/9a44662c-d855-42d4-a23f-e97274f37e7f.png)

And you can see spinner item, Canada city is highlighted.

![Spinner Control in Android](https://www.mindstick.com/mindstickarticle/c5e8b081-e855-413d-b8d6-0ff084193d5c/images/9658f379-1e83-44b0-b5e6-3d7d0a6286a2.png)

Thanks for reading this article. You can enter your valuable comments and suggestion to improve this article in the comment box.

---

Original Source: https://www.mindstick.com/articles/1387/spinner-control-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
