In this article I am going to explain the Spinner control and how to add data as well as creating selector for selected Item.

The spinner controls 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 over List View if you want to save space.

Follow the steps which are given below and you can use this example for learning the concepts of Spinner Selector.

  •   Open Eclipse
  •   Select New Android Application Project
  •    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=""

xmlns: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="" 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/

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" };
protected void onCreate(Bundle savedInstanceState) {
spin = (Spinner) findViewById(;
//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));
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, 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(); }
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
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
int selected = spin.getSelectedItemPosition();
if (position == selected) {
if (selected == 0) {
} else {
//Set the background color of selected item.
return spinnerItem;

 Now Run the Android application

Choose any one of city (as shown below).

Spinner Control in Android

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

Spinner Control in Android

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


Spinner Control in Android

  Thanks for reading this article. You can enter your valuable comments and suggestion to improve this article in the comment box.


  Modified On Mar-23-2018 06:02:26 AM

Leave Comment