Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog how to handle click outside of dialog view

I have AlertDialog and there are some buttons. By clicking them, some function/method begin to work.

But what to do if user clicks outside of AlertDialog view?

I want to start specific function then user clicks at empty field (outside of dialog view)

enter image description here

like image 577
Pavel Malinovskij Avatar asked Nov 29 '25 19:11

Pavel Malinovskij


1 Answers

You have to use this -

dialog.setCanceledOnTouchOutside(true);

Then for executing your function on outside click of dialog, do like this -

dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        // dialog dismisses
        // Do your function here
    }
});
like image 103
Anupam Avatar answered Dec 01 '25 08:12

Anupam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!