10

How can I display a message box in Xamarin.Android? How can I get the yes or no response from the message box?

--- Updated :

myBtn.Click += (sender, e) => 
{
   new AlertDialog.Builder(this)
   .SetMessage("hi")
   .Show();
};
Alex Wiese
  • 8,142
  • 6
  • 42
  • 71
MilkBottle
  • 4,242
  • 13
  • 64
  • 146

2 Answers2

20

You can use the AlertDialog.Buider class from within an Activity.

new AlertDialog.Builder(this)
    .SetPositiveButton("Yes", (sender, args) =>
    {
        // User pressed yes
    })
    .SetNegativeButton("No", (sender, args) =>
    {
        // User pressed no 
    })
    .SetMessage("An error happened!")
    .SetTitle("Error")
    .Show();
Alex Wiese
  • 8,142
  • 6
  • 42
  • 71
  • Thanks. It works. But How I can make it looks better like the MessageBox in Windows phone ? The MessageBox has Height, width and background color. – MilkBottle May 21 '13 at 01:43
  • 1
    See the xml from [this](http://stackoverflow.com/a/13763132/1411687) answer and use the constructor from [this](http://stackoverflow.com/a/3119581/1411687) one. Note that prior to API level 11 you must use a `ContextThemeWrapper` to theme the dialog as the newer constructor isn't available. – Alex Wiese May 21 '13 at 01:53
3

Try this:

public void ShowAlert (string str)
        {
            AlertDialog.Builder alert = new AlertDialog.Builder (this);
            alert.SetTitle (str);
            alert.SetPositiveButton ("OK", (senderAlert, args) => {
                // write your own set of instructions
                });

            //run the alert in UI thread to display in the screen
            RunOnUiThread (() => {
                alert.Show ();
            });
        }
Ram Ch. Bachkheti
  • 2,609
  • 2
  • 17
  • 14