Selection alerts

Sometimes we have to ask the user to select one or more items from a larger collection. This may be necessary as normal dialogs only have up to three buttons, but a fully customized UI is unnecessary.

How to do it...

To provide a list of items in a dialog, we can specify the items in much the same way as a simple message:

  1. If we need to provide the user with a selection of items in a dialog, we can also use the AlertDialog instance and the SetItems() method:
    string[] items = new string[] { ... }
    using (var dialog = new AlertDialog.Builder(this)) {
      dialog.SetTitle("Alert Title");
      dialog.SetPositiveButton("Close", delegate { 
      });
      dialog.SetItems(items, (s, e) => {
        int index = e.Which;
      });
      dialog.Show();
    } 
  2. We can also use data adapters ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.