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...

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. <string-array name="dropdown_arrays">
  6. <item>Spain</item>
  7. <item>USA</item>
  8. <item>Italy</item>
  9. <item>Japan</item>
  10. <item>Germany</item>
  11. <item>England</item>
  12. </string-array>
  13. </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. namespace AppDropDownList
  5. {
  6. [Activity(Label = "AppDropDownList", MainLauncher = true, Icon = "@drawable/icon")]
  7. public class MainActivity : Activity
  8. {
  9. protected override void OnCreate(Bundle bundle)
  10. {
  11. base.OnCreate(bundle);
  12. // Set our view from the "main" layout resource
  13. SetContentView(Resource.Layout.Main);
  14. var spin = FindViewById<Spinner>(Resource.Id.spinner1);
  15. spin.ItemSelected += (s, e) =>
  16. {
  17. Toast.MakeText(this, "Você selecionou " + e.Parent.GetItemAtPosition(e.Position).ToString(), ToastLength.Short).Show();
  18. };
  19. }
  20. }
  21. }
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