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.
If we want a decision to be made, we can use a pop-up dialog. This is achieved by using the AlertDialog.Builder
type:
AlertDialog.Builder
type:using (var dialog = new AlertDialog.Builder(this)) { dialog.SetTitle("Alert Title"); dialog.SetMessage("Alert message text here..."); dialog.Show(); }
SetPositiveButton()
, SetNegativeButton()
, or SetNeutralButton()
methods:dialog.SetPositiveButton("Yes", delegate { // do something cool here }); dialog.SetNegativeButton("No", delegate { // do something uncool here });
SetView()
method:var layout = LayoutInflater.Inflate( Resource.Layout.DialogLayout, null); dialog.SetView(layout);
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.
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.
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 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.