Alert dialogs

In many apps, we will require some input from the user or a decision to be made. The decision may require a response before the app can continue.

How to do it...

If we want a decision to be made, we can use a pop-up dialog. This is achieved by using the AlertDialog.Builder type:

  1. Alerts are easy to create using the AlertDialog.Builder type:
    using (var dialog = new AlertDialog.Builder(this)) {
      dialog.SetTitle("Alert Title");
      dialog.SetMessage("Alert message text here...");
      dialog.Show();
    }
  2. We can also add buttons to the alert by using the SetPositiveButton(), SetNegativeButton(), or SetNeutralButton() methods:
    dialog.SetPositiveButton("Yes", delegate { // do something cool here }); dialog.SetNegativeButton("No", delegate { // do something ...

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.