Create A Spinner In Xamarin Android


              XAMARIN Android Development

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 develop the spinner application in xamarin android application, you should follow the below steps.
Step 1

Open the visual studio 2015 update 3 and click the File and NewProject and open the new window, it should be following shortcut keys(Ctrl+Shift+N)



Step 2

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



Step 4

Now,open the project and go to solution explorer and click the Resource  and click the Layout and click the Main.xaml.

Step 5

Here, open the designer page and click the View and Toolbox and if you want some option, it is like that spinner and textview to drag and drop the method and build the design.

 

Step 6


Now ,go to the Source build the XAML code.

Here,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 to string.xml page and build code, it should be like that array's value.



Step 8
Here,write on string value. It is like items(c,c++,c#,ASP.NET
We can see that XML code.
  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 sharp code.


Here,c#(sharp) 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, go to run the project, it should be follow the shortcut keys F5 and run Android mobile phone model(Nokia X).

Step 11
Here, you can see that output.

 
Here,you can choose the language.

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



Presented By:
Abdul Rauf



Create A Spinner In Xamarin Android Create A Spinner In Xamarin Android Reviewed by Unknown on 1:41 AM Rating: 5

No comments:

Thanks

AGRSOFT. Powered by Blogger.