We shall create a simple Alert Dialog in Xamarin.Android
Now considering you have gone through the getting started guide from the documentation.
You must be having the project structure like this:
Your Main Activity must be looking like this:
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
}
}
Now What we shall do is, instead of adding one to the counter on button click, we shall ask user if he wants to add or substract one in a simple Alert Dialog
And on Click of the Positive or the negative button we will take the action.
button.Click += delegate {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetTitle("Specify Action");
alert.SetMessage("Do you want to add or substract?");
alert.SetPositiveButton("Add", (senderAlert, args) =>
{
count++;
button.Text = string.Format("{0} clicks!", count);
});
alert.SetNegativeButton("Substract", (senderAlert, args) =>
{
count--;
button.Text = string.Format("{0} clicks!", count);
});
Dialog dialog = alert.Create();
dialog.Show();
};
screenshot: