articles

Home / DeveloperSection / Articles / Spinner Control in Android

Spinner Control in Android

Manoj Pandey 6599 26-Mar-2014

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="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

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.

 


Updated 07-Sep-2019

Leave Comment

Comments

Liked By