Download Code

Xamarin Spinners Example Android

In this tutorial you will learn how to create xamarin spinners example android application.Let me give you some information about Spinners.

Xamarin Spinners

Xamarin Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. On touching displays a drop down menu with all other available values, from which the user can select a new one.For More detail Link.

You will create two kind of Spinner.

  • Using XML
  • Programmatically

Array Adapter

A concrete BaseAdapter that is backed by an array of arbitrary objects Link.

Alright let’s begin.Create new android application project.

Go to Main.axml.Add two TextViews and two Spinner in layout.

xamarin spinner example, xamarin spinner android, xamarin spinner android example source code, how to add items dynamically in spinner xamarin android, how to add items programmatically in spinner android, spinners,Xamarin Spinners,Xamarin Spinners example,Spinners example android

Next go to your Resources > values > strings.xml file.Add array in resources.

xamarin spinner example, xamarin spinner android, xamarin spinner android example source code, how to add items dynamically in spinner xamarin android, how to add items programmatically in spinner android, spinners,Xamarin Spinners,Xamarin Spinners example,Spinners example android

Go to your MainActivity.cs class.Do the following steps.

  • Create spinners variable.
  • Refer that spinner.
  • Create ArrayAdapter,pass array and spinners layout style.
  • Set Drop Down View of Spinner.
  • Set adapter to spinner.

Now for Programmatic Spinner.Do the following steps.

  • Create spinners variable.
  • Refer that spinner.
  • Create ArrayAdapter,pass IListArray and spinner layout style.
  • Set Drop Down View of Spinner.
  • Set adapter to spinners.
namespace XamarinSpinner
{
	[Activity(Label = "XamarinSpinner", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		
		Spinner staticSpin, dynamicSpin;

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			initialize();
			// Create an ArrayAdapter using the string array and a default spinner
			ArrayAdapter adapt = ArrayAdapter.CreateFromResource(this, Resource.Array.expression, Android.Resource.Layout.SimpleSpinnerItem);
			// Specify the layout to use when the list of choices appears
			adapt.SetDropDownViewResource(Android.Resource.Layout.SimpleListItemChecked);
			// Apply the adapter to the spinner
			staticSpin.Adapter = adapt;

			IList personName = new ArrayList();

			personName.Add("Aslam");
			personName.Add("Ali");
			personName.Add("Ayyan");
			personName.Add("Sabir");
			personName.Add("Nuashad");
			personName.Add("Aryan");
			personName.Add("Salman");
			personName.Add("Allen");
			personName.Add("Zain");
			personName.Add("Inestia");
			personName.Add("Bilal");
			personName.Add("Aliya");


			// Create an ArrayAdapter using the string array and a default spinner
			ArrayAdapter dynamicAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem , personName);
			// Specify the layout to use when the list of choices appears
			dynamicAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleListItemChecked);
			// Apply the adapter to the spinner
			dynamicSpin.Adapter = dynamicAdapter;


		}
		public void initialize()
		{
			staticSpin = (Spinner)FindViewById(Resource.Id.spinner1);
			dynamicSpin = (Spinner)FindViewById(Resource.Id.spinner2);
		}
	}
}

Now run your android application project.

xamarin spinner example, xamarin spinner android, xamarin spinner android example source code, how to add items dynamically in spinner xamarin android, how to add items programmatically in spinner android, spinners,Xamarin Spinners,Xamarin Spinners example,Spinners example android

That’s it for this tutorial Xamarin Spinners example.

For Java examples visit Android Codec.
Thanks.