How do I know if the user clicked outside AlertDialog?

1

My question is quite simple, but I'm not finding something I can use.

In my android project, I check the GPS connection of the user's device, and if it is not active, I ask for a dialog box to be activated, with two buttons, cancel and settings.

The button events are correct, but how do you know if the user, instead of clicking on some of the buttons, click outside of alertDialog, that is, on any screen space?

    
asked by anonymous 10.04.2015 / 15:08

1 answer

2

It is possible to make AlertDialog Modal this is to not be able to exit 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 AlertDialog to be canceled, to intersect this event, and to 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
        }
    });
    
10.04.2015 / 15:30