Creating A DropDown List Using Spinner In Xamarin Android App Using Visual Studio 2015

In this article, you will learn how to create a DropDown list, using Spinner in Android app, using Visual Studio 2015.

Introduction

Xamarin is a platform to develop the cross-platform and multi-platform apps. (Ex. Windows phone, Android, iOS) in Xamarin platform, where code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Spinner is a widget similar to a dropdown list to select the items.

Prerequisites
  • Visual Studio 2015 Update 3
The steps, given below are required to be followed in order to create a DropDown list, using Spinner in Android app, using Visual Studio 2015.

Step1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio or click (Ctrl+Shift+N).
 
 
Step 2

After opening the New project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app; a name (Ex:sample) and give path of your project. Click OK.

 
 
Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and source in your project.

Now, select Resource-->Layout-->double click to open main.axml page. You need to select the source to write XAML code.

If you want design, choose the designer Window and you can design your app.

 
 
Step 4

Afterwards, open the main.axml file and it will open the main page designer. In this page, you can design, as per the page type you want.

 
 
Now, delete the default hello world button.

Go to the source panel and you can see the button coding. You need to delete it.

Afterwards, delete XAML code. Now, delete C# button action code.

Go to the MainActivity.cs page. You need to delete the button code.

Step 5

Now, go to the toolbox Window in the toolbox Window and get all the types of the tools and control. You will go to the toolbox Window. Now, scroll down and you will see all tools the and controls.

You need to drag and drop the TextView.
 
 
 
Step 6

You need to drag and drop the Spinner.

 
Step 7

Now, go to the properties Window. You need to edit Spinner Id value and prompt value (Ex:android:id="@+id/spinner" android:prompt="@string/car_prompt).

 
 
Step 8

In this step, edit the TextView text Value (EX: android:text="@string/car_prompt").

 
Step 9

In this step, go to the Main.axml page Source Panel. Note the Id value and prompt value.

 

Main.axml
  1. <TextView  
  2.    android:layout_width="fill_parent"  
  3.    android:layout_height="wrap_content"  
  4.    android:layout_marginTop="10dip"  
  5.    android:text="@string/car_prompt" />  
  6. <Spinner  
  7.    android:id="@+id/spinner"  
  8.    android:layout_width="fill_parent"  
  9.    android:layout_height="wrap_content"  
  10.    android:prompt="@string/car_prompt" />  
Step 10

In this step, open the String.xml page. Go to Solution Explorer-->Resource-->values-->String.xml.
 
 

Step 11

After opening the String.xml file, write XML code, given below.
 
String.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.    <string name="car_prompt">Choose a Car</string>  
  4.    <string-array name="car_array">  
  5.    <item>Scorpio</item>  
  6.    <item>xylo</item>  
  7.    <item>Innovo</item>  
  8.    <item>Benz</item>  
  9.    <item>BMW</item>  
  10.    <item>Audi</item>  
  11.    <item>Jaguar</item>  
  12.    <item>ferrari</item>  
  13. </string-array>  
  14. </resources>  
 
 
Step 12

Now, go to the MainActivity.cs page. Write the code, given below.

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle)  
  2. {  
  3.     base.OnCreate(bundle);  
  4.     // Set our view from the "main" layout resource  
  5.     SetContentView(Resource.Layout.Main);  
  6.     Spinner spinner = FindViewById < Spinner > (Resource.Id.spinner);  
  7.     spinner.ItemSelected += new EventHandler < AdapterView.ItemSelectedEventArgs > (spinner_ItemSelected);  
  8.     var adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.car_array, Android.Resource.Layout.SimpleSpinnerItem);  
  9.     adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);  
  10.     spinner.Adapter = adapter;  
  11. }  
  12. private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e) {  
  13.     Spinner spinner = (Spinner) sender;  
  14.     string toast = string.Format("Selected car is {0}", spinner.GetItemAtPosition(e.Position));  
  15.     Toast.MakeText(this, toast, ToastLength.Long).Show();  
  16. }   
 

Step 13

If you have Android Virtual device, run the app on it, else connect your Android phone and run the app on it.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

 
Output

After a few seconds, the app will start running on your phone. You can click the dropdown list.
 
 
  
Show the dropdown list. You can choose any car.
 
 
 
You will see the selected car.


Summary

Thus, this was the process of creating a dropdown list, using Spinner, using Visual Studio 2015 Update 3.