Setting dialog button clicklistener in Android Xamarin
Solution 1
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
Solution 2
You can use this class
using Android.App;
using System.Threading.Tasks;
public class Show_Dialog
{
public enum MessageResult
{
NONE = 0,
OK = 1,
CANCEL = 2,
ABORT = 3,
RETRY = 4,
IGNORE = 5,
YES = 6,
NO = 7
}
Activity mcontext;
public Show_Dialog(Activity activity) : base()
{
this.mcontext = activity;
}
/// <summary>
/// Messbox function to show a massage box
/// </summary>
/// <param name="Title">to show Title for your messagebox</param>
/// <param name="Message">to show Message for your messagebox</param>
/// <param name="result">to get result for your messagebox; OK=1, Cancel=2, ingnore=3, else=0</param>
/// <param name="SetInverseBackgroundForced">to Set Inverse Background Forced</param>
/// <param name="SetCancelable">to set force message box is cancelabel or no</param>
/// <param name="PositiveButton">to show Title for PositiveButton</param>
/// <param name="NegativeButton">to show Title for NegativeButton</param>
/// <param name="NeutralButton">to show Title for your NeutralButton</param>
/// <param name="IconAttribute">to show icon for your messagebox</param>
/// <returns></returns>
public Task<MessageResult> ShowDialog(string Title, string Message, bool SetCancelable = false, bool SetInverseBackgroundForced = false, MessageResult PositiveButton = MessageResult.OK, MessageResult NegativeButton = MessageResult.NONE, MessageResult NeutralButton = MessageResult.NONE, int IconAttribute = Android.Resource.Attribute.AlertDialogIcon)
{
var tcs = new TaskCompletionSource<MessageResult>();
var builder = new AlertDialog.Builder(mcontext);
builder.SetIconAttribute(IconAttribute);
builder.SetTitle(Title);
builder.SetMessage(Message);
builder.SetInverseBackgroundForced(SetInverseBackgroundForced);
builder.SetCancelable(SetCancelable);
builder.SetPositiveButton((PositiveButton != MessageResult.NONE) ? PositiveButton.ToString() : string.Empty, (senderAlert, args) =>
{
tcs.SetResult(PositiveButton);
});
builder.SetNegativeButton((NegativeButton != MessageResult.NONE) ? NegativeButton.ToString() : string.Empty, delegate
{
tcs.SetResult(NegativeButton);
});
builder.SetNeutralButton((NeutralButton != MessageResult.NONE) ? NeutralButton.ToString() : string.Empty, delegate
{
tcs.SetResult(NeutralButton);
});
Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
});
Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
builder.Show();
});
// builder.Show();
return tcs.Task;
}
}
and you can use async or sync fuction
private void a()
{
Show_Dialog msg = new Show_Dialog(this);
msg.ShowDialog("Error", "Message");
}
or
private async void b()
{
Show_Dialog msg1 = new Show_Dialog(this);
if (await msg1.ShowDialog("Error", "Message", true, false, Show_Dialog.MessageResult.YES, Show_Dialog.MessageResult.NO) == Show_Dialog.MessageResult.YES)
{
//do anything
}
}
mathkid91
Updated on June 20, 2022Comments
-
mathkid91 almost 2 years
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?
-
mathkid91 almost 8 yearsThis looks like pretty much the same way I am using now. My problem is I can only access "Equals", "GetHashCode", "GetType" and "ToString" when I write "sender." in VIsual Studio. Do I need to cast it or something?? PS: Cant access link, requires login.
-
mathkid91 almost 8 yearsThank you! :) var dialog = sender as AlertDialog; is exactly what I wanted :)