bellotas
bellotas

Reputation: 2471

How to know when is clicked outside the dialog box - Android

I am working with a dialog, and two negativeButton and positiveButton. Nevertheless, I would like to change the behaviour when pressing outside the box. I have checked, and all the questions I have found about this topic are about hiding the AlertDialog when clicking outside dialog.dimiss(), but this is not my question. I want to know if there is a listener which allows me to switch this behaviour (would like to call other methods and remove other variables).

JAVA

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(m_activity);
    alertDialogBuilder.setTitle("Navigation");
    alertDialogBuilder.setMessage("Choose Mode");
    alertDialogBuilder.setNegativeButton("Navigation", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialoginterface, int i) {
            m_navigationManager.startNavigation(m_route);
            m_map.setTilt(60);
            startForegroundService();
        }

        ;
    });
    alertDialogBuilder.setPositiveButton("Simulation", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialoginterface, int i) {
            m_activity.findViewById(R.id.address_form).setVisibility(View.INVISIBLE);
            m_navigationManager.simulate(m_route, 30);//Simualtion speed is set to 45 m/s
            m_map.setTilt(60);
            startForegroundService();
        }
    });

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();

Upvotes: 0

Views: 771

Answers (1)

Boken
Boken

Reputation: 5362

You can add DismissListener for example:

alertDialogBuilder.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {

    }
});

This callback will be called when the dialog is dismissed for any reason (also when user choose "positive" or "negative button)!

Upvotes: 4

Related Questions