SPA Developer
SPA Developer

Reputation: 221

Hide Alert Dialog in MonoDroid

I am working on a MonoDroid app. I'm having some syntactical problems. I'm trying to create a utility method that will allow me to show an "Alert" dialog. I can successfully show the dialog. However, I can't figure out how to wireup the button event handler so that I can just "close" or dismiss the dialog. Currently, I have the following:

public static void ShowAlert(Activity activity, string title, string message)
{
  var dialogBuilder = new AlertDialog.Builder(activity);
  dialogBuilder.SetTitle(title);
  dialogBuilder.SetMessage(message);

  // Add the dialog buttons
  dialogBuilder.SetPositiveButton(Android.Resource.String.OK, delegate { });
  dialogBuilder.SetCancelable(true);

  // Display the dialog
  var alertDialog = dialogBuilder.Create();
  alertDialog.Show();
}

How do I make it so when a user clicks "OK", the alert dialog dismisses?

Thank you!

Upvotes: 0

Views: 431

Answers (1)

TRock
TRock

Reputation: 189

You will need to do two things. 1. you will need to set a handler for your positive button. I like an event handler, but your delegate is fine. 2. inside your event handler or delegate you will call .Hide on your dialog box.

If you are using an event handler then remember that you will not have access to your dialog box outside of the function you created it in. For this reason I always make a activity level variable that I use to hold my dialog box. Then I can just set it as new like you have done, then dismiss it in the event handler.

Upvotes: 1

Related Questions