Forum Xamarin.Android

Android modal AlertDialog


Is there a way to perform a Modal dialog on Android like in .NET

DialogResult myDialogResult = MessageBox.Show("My text", "My caption", MessageBoxButtons.YesNoCancel);

In Android when i called alertDialog.Show() the execution of program doesn't stop.

  • I try to Use Android.OS.Looper.Loop(), it works but i don't know how to exit with it.
  • I try to Use
    var are = new System.Threading.AutoResetEvent(false);
    But the dialog is not displayed.

Thanks in advanced


  • JoeJoe AUMember

    You don't want to block the UI thread. It handles handles the events from your dialog.

    What specifically are you trying to prevent from executing while the dialog is active?

  • LolocasLolocas FRMember

    Hi, thanks for your answer

    I want to do something like this

    if (Dialog.ShowYesNo() == "YES")
    // Action
    return true;
    return false;

    If possible i don't want to use Callback

  • TeHaTeHa DEMember ✭✭✭

    I've got the same problem a time ago and that's why I derive my own MessageBox-class from alertDialog and added a Result-Event.
    After the UI has called the MsgBox the UI can stop working and continue after receive the result-event.

  • @TeHa Can u share this class ? I need to know ho to do a Modali Dialog.
    a way to have the app wait for the user input:

    var builder = new AlertDialog.Builder(this);
    builder.SetTitle("Operation confirmation");
    builder.SetMessage ("Continue with " + cmd + " command?");
    builder.SetPositiveButton("Yes", (sender, args) => { /* do stuff on OK */ });
    builder.SetNegativeButton("No", (sender, args) => { cmd="cancel"; });
    builder.Show ();
  • TeHaTeHa DEMember ✭✭✭

    Sry, but I can't! It's very long ago and don't find this class anymore BUT I think it was anyway slightly oversized, means:
    You can do the same by calling the UI to proceed in the positive-button event. May look like this:

    private void someMethod()
        // ...
        // Do some stuff on UI
        // Build the Dialog ...
        builder.SetPositiveButton(GetString(Android.Resource.String.Yes), (sender, args) => { ResumeAfterDialog(); });
        // Don't do anything more
    private void ResumeAfterDialog()
        // go on

    PS: You can use android default strings for dialog buttons -> GetString(Android.Resource.String.Yes)

Sign In or Register to comment.