Forum Xamarin.Android

Setting AlertDialog.Builder buttons in the correct order

Hey guys

I'm creating an AlertDialog.Builder with the options of Yes, No, and Cancel. I've placed the code in the order I would like the buttons to be arranged in, but when I run an emulator the order is No, Cancel, Yes. How can I rearrange this?

Tagged:

Posts

  • LjusnanLjusnan DEMember ✭✭✭

    You have three buttons: positive, neutral and negative (I think this is the order). You can rename them as you like.

  • YkshLeoYkshLeo USMember ✭✭✭✭

    @DarthFlarth :

    Sample Code :

    void methodInvokeBaseAlertDialog (object sender, EventArgs e)
    {
     var dlgAlert = (new AlertDialog.Builder (this)).Create ();
     dlgAlert.SetMessage ("Hello from alertDialog");
     dlgAlert.SetTitle ("Alert Dialog");
     dlgAlert.SetButton ("OK", handllerNotingButton);
     dlgAlert.SetButton2 ("Cancel", handllerNotingButton);
     dlgAlert.SetButton3 ("Nothing", handllerNotingButton);
     dlgAlert.Show ();
    }
    
    
    void handllerNotingButton (object sender, DialogClickEventArgs e)
    {
        AlertDialog objAlertDialog = sender as AlertDialog;
        Button btnClicked = objAlertDialog.GetButton (e.Which);
        Toast.MakeText (this, "you clicked on " + btnClicked.Text, ToastLength.Long).Show ();
    }
    

    Output :

    I think the 3 buttons are formatted as : positive, negative and neutral. You can do the assign the name and events of the button as you like.

  • Thanks for the help!

  • RePinheiroRePinheiro Member ✭✭
    edited March 2019

    Just by complementing the friend's response:

    @YkshLeo said:

    @DarthFlarth :

    Sample Code :

    void methodInvokeBaseAlertDialog (object sender, EventArgs e)
    {
     var dlgAlert = (new AlertDialog.Builder (this)).Create ();
     dlgAlert.SetMessage ("Hello from alertDialog");
     dlgAlert.SetTitle ("Alert Dialog");
     dlgAlert.SetButton ("OK", handllerNotingButton);
     dlgAlert.SetButton2 ("Cancel", handllerNotingButton);
     dlgAlert.SetButton3 ("Nothing", handllerNotingButton);
     dlgAlert.Show ();
    }
    
    
    void handllerNotingButton (object sender, DialogClickEventArgs e)
    {
      AlertDialog objAlertDialog = sender as AlertDialog;
      Button btnClicked = objAlertDialog.GetButton (e.Which);
      Toast.MakeText (this, "you clicked on " + btnClicked.Text, ToastLength.Long).Show ();
    }
    

    Output :

    I think the 3 buttons are formatted as : positive, negative and neutral. You can do the assign the name and events of the button as you like.

    In the button action, you can also use this way

    alert.SetButton("Yes", (c, ev) =>
    {
       //Action
    });
       alert.SetButton2("No", (c, ev) =>
    {
        //Action
     });> 
    
Sign In or Register to comment.