Create A Spinner In Xamarin Android Application Using Visual Studio 2015 Update 3

Introduction

This article is about how to create a Spinner application with TextView in Xamarin Android application, using Visual Studio 2015 Update 3.
 
Requirements
  • Visual Studio 2015 Update 3
If you want to develop the Spinner application in Xamarin Android application, you should follow the below steps.
 
Step 1

Open Visual Studio 2015 update 3 and click File >> NewProject and open the new window. Or, use the shortcut keys - Ctrl+Shift+N.
 
 
Step 2

Now, open the New project and go to the Visual C# . Click Android, then choose the BlankApp (Android). Now, write the application name and click on OK button.



Step 4

Now,open the project, go to Solution Explorer, and click the Resource. Click Layout >> Main.xaml.

 
Step 5

Here, open the designer page and click the View and Toolbox. If you want some options like Spinner and TextView, drag and drop the method and build the design.

 

Step 6


Now, go to the Source and build the XAML code.

 
Here is the XAML code.
  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.     <TextView  
  7.         android:text="@string/language_prompt"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/textView1" />  
  11.   <Spinner  
  12.       android:layout_width="match_parent"  
  13.       android:layout_height="wrap_content"  
  14.       android:id="@+id/spinner1"  
  15.       android:prompt="@string/language_prompt"/>  
  16.       </LinearLayout>  
Step 7

Now, go to Values and select string.xml page and build code. It should be like that array's value.



Step 8

Here, write on string value, like C, C++, C#, ASP.NET

 
Now, the XML code will be -
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.    <string name="language_prompt">Choose a Language</string>  
  4.  <string-array name="language_array">  
  5.    <item>C</item>  
  6.        <item>C++</item>  
  7.        <item>JAVA</item>  
  8.        <item>C#</item>  
  9.        <item>ASP.NET</item>  
  10.        <item>PERL</item>  
  11.        <item>PHP</item>  
  12.        <item>SQL</item>  
  13.  </string-array>  
  14. </resources>  
Step 9

Now, go to MainActivity.cs and fill and build the C# code.

  1. using System;  
  2. using Android.App;  
  3. using Android.Content;  
  4. using Android.Runtime;  
  5. using Android.Views;  
  6. using Android.Widget;  
  7. using Android.OS;  
  8. using Android.Text;  
  9. namespace SpinnerApp  
  10. {  
  11.     [Activity(Label = "SpinnerApp", MainLauncher = true, Icon = "@drawable/icon")]  
  12.     public class MainActivity : Activity  
  13.     {  
  14.           
  15.         protected override void OnCreate(Bundle bundle)  
  16.         {  
  17.             base.OnCreate(bundle);  
  18.   
  19.             // Set our view from the "main" layout resource  
  20.             SetContentView(Resource.Layout.Main);  
  21.   
  22.             //SetContentView(Resource.Layout.Main);  
  23.   
  24.   
  25.             Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner1);  
  26.   
  27.             spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);  
  28.             var adapter = ArrayAdapter.CreateFromResource(  
  29.                     this, Resource.Array.language_array, Android.Resource.Layout.SimpleSpinnerItem);  
  30.   
  31.             adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);  
  32.             spinner.Adapter = adapter;  
  33.         }  
  34.         private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)  
  35.         {  
  36.             Spinner spinner = (Spinner)sender;  
  37.   
  38.             string toast = string.Format("The Language is {0}", spinner.GetItemAtPosition(e.Position));  
  39.             Toast.MakeText(this, toast, ToastLength.Long).Show();  
  40.         }  
  41.     }  
  42.     }  
Step 10

Now, Run the project by following the shortcut key F5. I am running the app in Android mobile phone model (Nokia X).

 
Step 11

Here, you can see the output.

 
 
Here, you can choose the language.

 
Here,you can click the language and after that, you will get the final output.