I want to display a dialog alert in my Xamarin Android app (C#), and I want to do stuff to the dialog when I click on the buttons.
From before, I use this code:
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.SetTitle("Delete")
.SetMessage("Are you sure you want to delete?)
.SetPositiveButton("No", (senderAlert, args) => { })
.SetNegativeButton("Yes", (senderAlert, args) => {
DatabaseHelper.Delete(item);
});
builder.Create().Show();
To make a random example, lets say I want to keep the dialog box open until the item is deleted, but I want to disable the Yes button and change the message text while Android is working. Is this possible from the code I have to access the dialog and change it? Neither senderAlert nor args have any useful properties or methods.
I have been looking for other ways to build my dialog, and I have seen these two:
1) This guy is using the way bellow, but my DialogInterface does not have a .OnClickListener()
builder.setPositiveButton("Test",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Do stuff to dialog
}
});
2) This guy is using IDialogInterfaceOnClickListener, and I have been trying to find an example of how to do it this way, but I havent found any. Seems like he is using null instead of the code I would want.
.setPositiveButton("OK", (Android.Content.IDialogInterfaceOnClickListener)null)
Any ideas?
I use something like this:
using (var builder = new AlertDialog.Builder(Activity))
{
var title = "Please edit your details:";
builder.SetTitle(title);
builder.SetPositiveButton("OK", OkAction);
builder.SetNegativeButton("Cancel", CancelAction);
var myCustomDialog = builder.Create();
myCustomDialog.Show();
}
private void OkAction(object sender, DialogClickEventArgs e)
{
var myButton = sender as Button; //this will give you the OK button on the dialog but you're already in here so you don't really need it - just perform the action you want to do directly unless I'm missing something..
if(myButton != null)
{
//do something on ok selected
}
}
private void CancelAction(object sender, DialogClickEventArgs e)
{
//do something on cancel selected
}
example: https://wordpress.com/read/feeds/35388914/posts/1024259222
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With