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:
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(); }
We can also add buttons to the alert by using the
SetPositiveButton()
,SetNegativeButton()
, orSetNeutralButton()
methods:dialog.SetPositiveButton("Yes", delegate { // do something cool here }); dialog.SetNegativeButton("No", delegate { // do something uncool here });
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...