Reputation: 922
I need to run some code where my alert dialog gets closed other then by just dismissing it via clicking on the buttons. The user can close a dialog by clicking somewhere on the activity in the background.
I've managed to get this to work like here:
private void InfoAlert()
{
alert = new Android.App.AlertDialog.Builder(this);
alert.SetTitle(Resources.GetString(Resource.String.HiThere));
alert.SetMessage(Resources.GetString(Resource.String.BetaGame));
alert.SetNeutralButton(Resources.GetString(Resource.String.GotIt), (senderAlert, args) =>
{
var activity2 = new Intent(this, typeof(Activity_Login));
StartActivity(activity2);
Finish();
});
alert.SetOnCancelListener(new ProgressDialogCancel(this, this));
Dialog dialog = alert.Create();
dialog.Show();
}
public class ProgressDialogCancel : Java.Lang.Object, IDialogInterfaceOnCancelListener
{
private Context _context;
private readonly Activity _activity;
public ProgressDialogCancel(Context context, Activity activity)
{
_context = context;
_activity = activity;
}
public void OnCancel(IDialogInterface dialog)
{
Activity_AcctCreationLogin.btnInsta.Visibility = ViewStates.Visible;
Activity_AcctCreationLogin.btnFB.Visibility = ViewStates.Visible;
Activity_AcctCreationLogin.btnCreateAccount.Visibility = ViewStates.Visible;
Activity_AcctCreationLogin.btnExplain.Visibility = ViewStates.Visible;
Activity_AcctCreationLogin.btnLogin.Visibility = ViewStates.Visible;
Activity_AcctCreationLogin.ln1.SetBackgroundResource(0);
Activity_AcctCreationLogin.ln1.SetBackgroundColor(Color.ParseColor("#ffffff"));
Activity_AcctCreationLogin.alert.Dispose();
}
}
But since I would need to run so much code from the other class, this is a very unclean solution. Idealy, there would be a way of doing it like this:
alert.SetOnCancelListener += delegate
{
//do something
};
How would I achieve something like this?
Thanks :)
Upvotes: 0
Views: 306
Reputation: 11601
If I got you correctly, you need to handle DismissEvent like this:
var dialog = new Dialog(CONTEXT);
// SetContentView, SetTitle, ...
dialog.DismissEvent += (s, e) =>
{
// do whatever you need here, this will be called on
// dismiss (clicking on cancel button or outside of dialog)
};
Upvotes: 1
Reputation: 385
one solution might be- but do remember .setCanceledOnTouchOutside(true)
has to be called in onCreate method
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);
or override onTouchEvent()
and check for action type. if the action type is 'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_OUTSIDE){
System.out.println("dismiss dialog");
this.dismiss();
}
return false;
}
for other possible solutions refer: How to dismiss a DialogFragment when pressing outside the dialog?
OR Allow outside touch for DialogFragment
Upvotes: 1