AlertDialog's setCancelable(false) method not working

Solution 1:

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:

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

Solution 3:

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:

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.