Create A Spinner In Xamarin Android
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.
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.
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.

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.
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.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:text="@string/language_prompt"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView1" />
- <Spinner
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/spinner1"
- android:prompt="@string/language_prompt"/>
- </LinearLayout>
Now, go to Values and select string.xml page and build code. It should be like that array's value.
Step 8
Now, the XML code will be -
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="language_prompt">Choose a Language</string>
- <string-array name="language_array">
- <item>C</item>
- <item>C++</item>
- <item>JAVA</item>
- <item>C#</item>
- <item>ASP.NET</item>
- <item>PERL</item>
- <item>PHP</item>
- <item>SQL</item>
- </string-array>
- </resources>
Step 9
Now, go to MainActivity.cs and fill and build the C# code.

Now, go to MainActivity.cs and fill and build the C# code.
- using System;
- using Android.App;
- using Android.Content;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.OS;
- using Android.Text;
- namespace SpinnerApp
- {
- [Activity(Label = "SpinnerApp", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.Main);
- //SetContentView(Resource.Layout.Main);
- Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner1);
- spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);
- var adapter = ArrayAdapter.CreateFromResource(
- this, Resource.Array.language_array, Android.Resource.Layout.SimpleSpinnerItem);
- adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
- spinner.Adapter = adapter;
- }
- private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
- {
- Spinner spinner = (Spinner)sender;
- string toast = string.Format("The Language is {0}", spinner.GetItemAtPosition(e.Position));
- Toast.MakeText(this, toast, ToastLength.Long).Show();
- }
- }
- }
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.

Create A Spinner In Xamarin Android
Reviewed by Unknown
on
12:36 PM
Rating:
Nice blog thanks for sharing the valuable information.
ReplyDeleteHire Xamarin Developer