AlertDialog's setCancelable(false) method not working

AndroidAndroid Alertdialog

Android Problem Overview


I had created an AlertDialog which is working fine. It is disappearing, if I press:

  1. escape keyboard button or
  2. back button using mouse
    To make it stay focused even on above stated conditions, I had added '.setCancelable(false)' statement while building. But, I still see dialog disappearing. Where is the problem? Please help.

Code added:

return new AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setCancelable(false)
                .setPositiveButton(R.string.alert_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((FragmentAlertDialog)getActivity()).doPositiveClick();
                        }
                    }
                )
                .setNegativeButton(R.string.alert_dialog_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((FragmentAlertDialog)getActivity()).doNegativeClick();
                        }
                    }
                )
                .create();


Env: Android 4.0 on XP Professional.

Android Solutions


Solution 1 - Android

Is this your complete code? then please change your code for setting setCancelable(false) like this

void showDialog() {
    DialogFragment newFragment = MyAlertDialogFragment.newInstance(
            R.string..alert_dialog_two_buttons_title);
    newFragment.setCancelable(false);
    newFragment.show(getFragmentManager(), "dialog");
}

Solution 2 - Android

Your dialog is set to no-cancelable, but your host fragment is still cancelable. Set your fragment with setCancelable(false).

Solution 3 - Android

Another working example:

Step 1

Create class:

public class DialogActivity extends android.app.DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.myMessage);
        setCancelable(false);
        return builder.create();
   }
}

Step 2

Add method to your Activity:

private boolean showDialog() {
    FragmentManager manager = getFragmentManager();
    DialogActivity dialogActivity;
    dialogActivity = new DialogActivity();
    dialogActivity.show(manager, "DialogActivity");
    return true;
}

Step 3

Call showDialog() when you need to show dialog

Solution 4 - Android

dialog.setCanceledOnTouchOutside(false);

setCanceledOnTouchOutside(boolean)

> Sets whether this dialog is canceled when touched outside the window's bounds. If setting to true, the dialog is set to be cancelable if not already set.

Solution 5 - Android

The simplest way to implement "setCancelable" is to implement the same when calling the dialog in the activity; That way, not directly in the dialog class.

 Dialog myDialog = new Dialog();
        myDialog.setCancelable( false );
        myDialog.show( getSupportFragmentManager(),"dialog" );
        return true;

Solution 6 - Android

In Kotlin for making dialog non-dismissible

dialog.isCancelable =false

Solution 7 - Android

Based on your AlertDialog type you can:

AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.MyAlertDialogLayout).setCancelable(false);

or

AlertDialog alertDialog = builder.create();

alertDialog.setCancelable(false);

Solution 8 - Android

In DialogFragment

Used:-

dialog.setCanceledOnTouchOutside(false)

Solution 9 - Android

        builder.setTitle("There's a problem")
                .setMessage(error)
                .setCancelable(true)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .setNegativeButton("", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .show();
            });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlupchiazoemView Question on Stackoverflow
Solution 1 - AndroidSandyView Answer on Stackoverflow
Solution 2 - AndroidGreenView Answer on Stackoverflow
Solution 3 - AndroidKrzysztof DziubaView Answer on Stackoverflow
Solution 4 - AndroidDoruChideanView Answer on Stackoverflow
Solution 5 - AndroidLaiane OliveiraView Answer on Stackoverflow
Solution 6 - Androidpratham kesarkarView Answer on Stackoverflow
Solution 7 - AndroidMohsen EmamiView Answer on Stackoverflow
Solution 8 - AndroidMirza AdilView Answer on Stackoverflow
Solution 9 - AndroidHashan MadusankaView Answer on Stackoverflow