Create android spinner dynamically in Xamarin

20,187

To dynamically create items for Spinner, you need to have an adapter.

The simplest adapter would be ArrayAdapter<T>.

Here is the example.

var items = new List<string>() {"one", "two", "three"};
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, items);

After setting up the adapter, find the Spinner in your view and set adapter to it.

var spinner = FindViewById<Spinner>(Resource.Id.spinner);
spinner.Adapter = adapter;
Share:
20,187
Anand
Author by

Anand

I'm a passionate mobile app developer. I works with Swift and Objective C to build native mobile applications. I also have experience with Hybrid mobile application development with Appcelerator Titanium. My contact is anandvlnd (skype)

Updated on July 20, 2021

Comments

  • Anand
    Anand almost 3 years

    I am creating a simple application to get familiar with Xamarin. I want to create and populate a spinner and display its options dynamically. I have seen the documentation here but it is not created programmatically. Any help will be appreciated

    var levels = new List<String>() { "Easy", "Medium", "Hard", "Multiplayer" };
    var adapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleSpinnerItem, levels);
                        adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
    var spinner = FindViewById<Spinner>(Resource.Id.spnrGameLevel);
    spinner.Adapter = adapter;
    
    spinner.ItemSelected += (sender, e) =>
    {
        var s = sender as Spinner;
        Toast.MakeText(this, "My favorite is " + s.GetItemAtPosition(e.Position), ToastLength.Short).Show();
    };
    

    My axml file

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Choose your game level" />
        <Spinner
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spnrGameLevel" />
    </LinearLayout>
    
  • Anand
    Anand over 10 years
    Thanks for your answer. I have tried the same, but getting an exception while tried this. Object Reference not set to an instance of an object. I have updated the my question with code
  • Aaron He
    Aaron He over 10 years
    @Anand Which statement caused the exception?
  • Anand
    Anand over 10 years
    Exception occurs in spinner.Adapter = adapter;
  • Aaron He
    Aaron He over 10 years
    @Anand Did you forget to instantiate the view contains the spinner?
  • Anand
    Anand over 10 years
    No, I have instantiated the view
  • Aaron He
    Aaron He over 10 years
    @Anand It's weird. I tried the same thing, just adding SetContentView(Resource.Layout.Main); in OnCreate method. And it works.
  • Mohammad Sina Karvandi
    Mohammad Sina Karvandi about 7 years
    It's because spinner is not invoked ,,, you should put SetContentView(Resource.Layout.Main); before using var spinner = FindViewById<Spinner>(Resource.Id.spinner);