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 uncool here
    });
  3. Custom views can also be used with alerts through the SetView() method:
    var layout = LayoutInflater.Inflate(
      Resource.Layout.DialogLayout, null);
    dialog.SetView(layout);

How it works...

An alert dialog is a pop-up window that appears and prompts the user to make a decision or select from a series of options. It does not fill the screen but is a modal window, meaning that although the user has to make a decision before proceeding, the current context hasn't switched away. Alert dialogs are a specific dialog type, usually used to display a three-button decision platter or a list of selectable items, but they can also have a totally custom layout.

Note

Alert dialogs interrupt user activity and require a response, so they should be used sparingly.

In order to create a dialog, we use the constructor of the AlertDialog.Builder type. Once we have an instance, we can provide a title using the SetTitle() method and provide a message using the SetMessage() method. The title is optional and should not be used if the dialog only displays a simple question.

If we want to provide the user with a choice using a set of buttons, we use the SetPositiveButton(), SetNegativeButton(), and SetNeutralButton() methods. Each method allows us to specify a button caption and a delegate that can be used to attach code to the button tap.

Each button type has a preferred purpose. The positive button should be used to accept or to continue with an action, and the negative button should be used to cancel the operation. The neutral button is used to provide an option that allows the user to neither accept nor cancel an operation. Such an action could be to postpone the operation.

We can also customize the dialog entirely by replacing the view with a custom layout. This is done by inflating a layout, or creating one, and assigning it to the dialog with the SetView() method. This allows for far greater flexibility and can be used to create more advanced dialogs.

Tip

The Android support libraries also provide an AlertDialog type, which is styled using the material design.

We can also make use of the Android support library to style our dialogs using the new material design. This dialog functions in exactly the same way as the native dialog, only with a modern theme. By using the AlertDialog type in the Android.Support.V7.App namespace, our dialogs will be styled to match the AppCompatActivity and Fragment themes.

There's more...

There are also two other types of dialogs: DatePickerDialog and TimePickerDialog. These dialogs have a predefined UI that allows the user to select a date or time.

Dialogs can be used directly, but they should be used with a DialogFragment instance. This fragment is specially designed as a container for the dialog and provides all the control needed for creating and displaying dialogs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.100.34