How do I know if the user clicked outside of Alertdialog?

Asked

Viewed 542 times

1

My doubt is very simple, but I’m not finding anything I can use.

In my android project, I check the GPS connection of the user’s device, and if case is not active, I ask in a dialog box for it activate, with two buttons, cancel and settings.

The event of the buttons are correct, but how to know if the user instead of clicking some of the buttons, click outside the alertDialog, ie in any screen space ?

1 answer

2


It is possible to make the Alertdialog Modal this is not possible to leave the dialog without the user choosing one of the buttons:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
.....
.....
builder.show();

Another way is to allow the Alertdialog can be canceled, intersect this event and act accordingly.

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setOnCancelListener(new OnCancelListener() {

    @Override
    public void onCancel(DialogInterface dialog) {
        dialog.dismiss();
        // Faça aqui o que pretende quando o dialog é cancelado
        }
    });
  • It worked properly, that’s just what I was wanting, again, thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.