Spinner is a control or widget similar to dropdown list for selecting items.
In this article I am going to explain how create a spinner control in an android by using Spinner widget. I also explain how to add list of items in a Spinner, when one is selected, a toast message will display the selected item.
- Start a new project, named SpinnerDemo.
- Open res/values/strings.xml and add the following resource in xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="country">Select a Country</string>
<string name="app_name">SpinnerDemo</string>
<string-array name="country_array">
<item>India</item>
<item>USA</item>
<item>Australlia</item>
<item>Newzeland</item>
<item>Italy</item>
<item>France</item>
<item>England</item>
<item>Srilanka</item>
</string-array>
</resources>
- Open res/layout/main.xml and add a Spinner widget inside the linear layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/country" />
<Spinner android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="@string/country" />
</LinearLayout>
· Now open the Activity file and insert the following code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class SpinnerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner=(Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this,
R.array.
R.array.country_array, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelected());
}
public class OnItemSelected implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?>arg0,View arg1,int arg2,long arg3)
{
{
Toast.makeText(arg0.getContext(), "The country is
"
"+arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
}
The Spinner widget is captured from the layout with findViewById(int). The createFromResource() method then creates a new ArrayAdapter, which binds each item in the string array to the initial appearance for the Spinner. The R.array.country_array ID references the string-array defined above and the android.R.layout.simple_spinner_item ID references a layout for the standard spinner appearance, defined by the platform.
Then setDropDownViewResource(int) is called to define the appearance for each item when the widget is opened. Finally, the ArrayAdapter is set to associate all of its items with the Spinner by calling setAdapter (adapter).
The nested class (OnItemSelected) implemented an OnItemSelectedListener interface which implemented onItemSelected (), calls when an item is selected from the spinner.
- Run the application
Your output looks like below:
A toast message will be displayed when an item is selected from the Spinner.
Leave Comment