Spinner
- PDF for offline use
Let us know how you feel about this
Translation Quality
0/250
last updated: 2017-05
Spinner
is a widget similar to a drop-down list for selecting items.
In this tutorial, you'll create a simple spinner widget that displays a list of planets. When one is selected, a toast message will display the selected item.
Start a new project named HelloSpinner.
Open the Resources/Layout/Main.axml file and insert the following:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="10dip" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="@string/planet_prompt" /> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:prompt="@string/planet_prompt" /> </LinearLayout>Notice that the
TextView'sandroid:textattribute and theSpinner'sandroid:promptattribute both reference the same string resource. This text behaves as a title for the widget. When applied to theSpinner, the title text will appear in the selection dialog that appears upon selecting the widget.Create a Strings.xml file in Resources/Values and edit the file to look like this:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="planet_prompt">Choose a planet</string> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> <item>Jupiter</item> <item>Saturn</item> <item>Uranus</item> <item>Neptune</item> </string-array> </resources>The
<string>element defines the title string referenced by theTextViewandSpinnerin the layout above. The<string-array>element defines the list of strings that will be displayed as the list in theSpinnerwidget.Now open the MainActivity.cs file and insert the following code for the
OnCreate()method:protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); // Set our view from the "Main" layout resource SetContentView (Resource.Layout.Main); Spinner spinner = FindViewById<Spinner> (Resource.Id.spinner); spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs> (spinner_ItemSelected); var adapter = ArrayAdapter.CreateFromResource ( this, Resource.Array.planets_array, Android.Resource.Layout.SimpleSpinnerItem); adapter.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem); spinner.Adapter = adapter; }After the
Main.axmllayout is set as the content view, theSpinnerwidget is captured from the layout withFindViewById<>(int). TheCreateFromResource()method then creates a newArrayAdapter, which binds each item in the string array to the initial appearance for theSpinner(which is how each item will appear in the spinner when selected). TheResource.Array.planets_arrayID references thestring-arraydefined above and theAndroid.Resource.Layout.SimpleSpinnerItemID references a layout for the standard spinner appearance, defined by the platform. ThenSetDropDownViewResourceis called to define the appearance for each item when the widget is opened (SimpleSpintemis another standard layout defined by the platform). Finally, theArrayAdapteris set to associate all of its items with theSpinnerby setting theAdapterproperty.Now provide a callback method that will notify your application when an item has been selected from the
Spinner. Here's what this method should look like:private void spinner_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e) { Spinner spinner = (Spinner)sender; string toast = string.Format ("The planet is {0}", spinner.GetItemAtPosition (e.Position)); Toast.MakeText (this, toast, ToastLength.Long).Show (); }When an item is selected, we cast the sender to a
Spinnerso we can access the its items. Using thePositionproperty on theItemEventArgs, we can find out the text of the selected object, and use it to display aToast.Run the application.
It should look like this:
Resources
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Let us know how you feel about this
Translation Quality
0/250
Xamarin Workbook
If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.


