Xamarin Android - Generating A Dropdownlist Using Widget Spinner

Today, I am going to share a simple tip about how generate a dropdownlist, using the widget Spinner in a Xamarin Android Application, using Visual Studio 2015 and Xamarin.

Let´s get started...

  • Create a new project, using VS 2015 community.
  • Select Visual C# language and Android-> Blank app (Android) template.
  • Open Main.axml in folder Resources/layout in design mode.
  • Open the toolbox and scroll down to TextView. Drag it onto the design surface. Repeat this step and put Spinner below TextView.

See the layout.

layout

Switch over to source view and do the changes in the code, which are given below.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.      android:orientation="vertical"  
  4.      android:layout_width="match_parent"  
  5.      android:layout_height="match_parent"  
  6.      android:minWidth="25px"  
  7.      android:minHeight="25px"  
  8.      android:background="@android:color/holo_blue_dark">  
  9.      <TextView  
  10.          android:text="Selecione o Estado"  
  11.          android:textAppearance="?android:attr/textAppearanceLarge"  
  12.          android:layout_width="match_parent"  
  13.          android:layout_height="wrap_content"  
  14.          android:id="@+id/textView1" />  
  15.      <Spinner  
  16.          android:padding="2dp"  
  17.          android:entries="@array/dropdown_arrays"  
  18.          android:backgroundTint="#d11f08"  
  19.          android:background="@android:color/holo_green_dark"  
  20.          android:layout_width="match_parent"  
  21.          android:layout_height="wrap_content"  
  22.          android:id="@+id/spinner1" />  
  23.  </LinearLayout>  
Notice, I’ve defined the entries property, assigning the name dropdown_arrays.

In the next step I'll create the name of array of items.

Open Strings.xml file in the folder values and include the code, given below (in Italics).
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="Hello">Hello World, Click Me!</string>  
  4.     <string name="ApplicationName">AppDropDownList</string>  
  5.    
  6.   <string-array name="dropdown_arrays">  
  7.     <item>Spain</item>  
  8.     <item>USA</item>  
  9.     <item>Italy</item>  
  10.     <item>Japan</item>  
  11.     <item>Germany</item>  
  12.     <item>England</item>  
  13.   </string-array>  
  14.     
  15. </resources>  
Here, I defined the array dropdown_arrays, which I’ll be using to show the items in Spinner.

Now, we are ready to put the things to work in the file MainActivity.cs.

Open MainActivity.cs file and put the code, given below.
  1. using Android.App;  
  2. using Android.OS;  
  3. using Android.Widget;  
  4.    
  5. namespace AppDropDownList  
  6. {  
  7.     [Activity(Label = "AppDropDownList", MainLauncher = true, Icon = "@drawable/icon")]  
  8.     public class MainActivity : Activity  
  9.     {  
  10.         protected override void OnCreate(Bundle bundle)  
  11.         {  
  12.             base.OnCreate(bundle);  
  13.    
  14.             // Set our view from the "main" layout resource  
  15.             SetContentView(Resource.Layout.Main);  
  16.             var spin = FindViewById<Spinner>(Resource.Id.spinner1);  
  17.    
  18.             spin.ItemSelected += (s, e) =>  
  19.             {  
  20.                 Toast.MakeText(this"Você selecionou " + e.Parent.GetItemAtPosition(e.Position).ToString(), ToastLength.Short).Show();  
  21.             };  
  22.         }  
  23.     }  
  24. }  
In this code, I only create an instance of my Spinner and used Spinner´s ItemSelected event, which I can set to an eventHandler, where I show the selected item, using Toast.

It is pretty simple and is shown below.

result

Up Next
    Ebook Download
    View all
    Learn
    View all