Skip to content
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
91 lines (67 sloc) 3.66 KB
layout title category
Android MvxSpinner

Available in MvvmCross 3.

MvxSpinner is a wrapper around Android's Spinner View, which provides a drop down menu kind of view, to quickly select one value.

Currently MvxSpinner supports binding to the following properties:

  • ItemsSource
  • HandleItemSelected

Getting started

Adding a MvxSpinner is fairly simple. You add the widget in your layout like so.

    local:MvxBind="ItemsSource Items; HandleItemSelected ItemSelectedCommand"

You will need to bind a ItemsSource and provide a item template and drop down item template for it.

Note: if you do not provide a Item Template or Drop Down Item Template, the default adapter will use Android.Resource.Layout.SimpleSpinnerItem as Item Template and Android.Resource.Layout.SimpleSpinnerDropDownItem for the Drop Down Item Template.

Using an Item Template and Drop Down Item Template

If you do not want the default look of the spinner or you want to bind multiple properties in your ViewModel, you will need to provide a custom Item Template and Drop Down Template.

The Item Template is for the selected item. The Drop Down Item Template is for the items, when the Spinner is expanded and shows the list of spinner choices.

For this example let us assume you want to fill your MvxSpinner with countries and you want to shown the country name and country flag in your template.

The ViewModel for the items in the Spinner could simply consist of a couple of properties, for briefness assume they are CountryName and CountryCode.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

        local:MvxBind="Drawable CountryCodeToDrawable(CountryCode)" />

        local:MvxBind="Text CountryName" />

Note: it is up to the reader to create the CountryCodeToDrawableValueConverter.

This layout would be possible to use both for Item Template and for Drop Down Item Template, making these look the same. If you want different appearances, adjust your layouts accordingly.

Given that we called the Item Template above country_item_template.axml it will be accessible as @layout/country_item_template for the view attributes.


HandlItemSelected command

The HandleItemSelected command, can be bound to a command in your ViewModel. When executed this will provide the ViewModel bound to the selected row as the command parameter.

private MvxCommand<CountryViewModel> _countrySelectedCommand;
public MvxCommand<DogViewModel> CountrySelectedCommand => _countrySelectedCommand = 
    _countrySelectedCommand ?? new MvxCommand<DogViewModel>(OnCountrySelectedCommand);

private void OnCountrySelectedCommand(CountryViewModel country)
    // write on country selected logic here
You can’t perform that action at this time.