DialogFragment setCancelable property not working

AndroidAndroid Dialogfragment

Android Problem Overview


I am working in an android application and am using a DialogFragment to show a dialog and I want to make that DialogFragment not cancelable. I have made the dialog cancelable property to false, but still its not affecting.

Please look into my code and suggest me a solution.

public class DialogTest extends DialogFragment {

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		return super.onCreateDialog(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.dialog_test, container, true);
		getDialog().requestWindowFeature(STYLE_NO_TITLE);
		getDialog().setCancelable(false);

		return view;
	}
 }

Android Solutions


Solution 1 - Android

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.dialog_test, container, true);
    getDialog().requestWindowFeature(STYLE_NO_TITLE);
    getDialog().setCancelable(false);

    return view;
}

instead of getDialog().setCancelable(false); you have to use directly setCancelable(false);

so the updated answer will be like this

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.dialog_test, container, true);
    getDialog().requestWindowFeature(STYLE_NO_TITLE);
    setCancelable(false);
    
    return view;
}

Solution 2 - Android

Use the following Snippet

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

and if you want to disable the out side touch around dialog use the following line of code

DialogFragment.getDialog().setCanceledOnTouchOutside(true);

Solution 3 - Android

In case you use alert builder (and probably in every case you wrap dialog inside a DialogFragment) to help build your dialog, please don't use getDialog().setCancelable(false) or Dialog.setCancelable(false) because it's not going to work. Use setCancelable(false) as shown in code below as it's mentioned in oficial android documentation:

public void setCancelable (boolean cancelable)

Added in API level 11 Control whether the shown Dialog is cancelable. Use this instead of directly calling Dialog.setCancelable(boolean), because DialogFragment needs to change its behavior based on this."

ref:http://developer.android.com/reference/android/app/DialogFragment.html#setCancelable(boolean)

public class MyDialogFragment extends DialogFragment {

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(R.layout.dialog_layout, null, false);
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
				.setTitle("in case you want use a title").setView(view);

		AlertDialog alert = builder.create();
		// alert.setCancelable(false); <-- dont' use that instead use bellow approach
		setCancelable(false); <-  press back button not cancel dialog, this one works fine
		alert.setCanceledOnTouchOutside(false); <- to cancel outside touch
		
		return alert;
}

Solution 4 - Android

Simple Solution in DialogFragment

Used

dialog.setCanceledOnTouchOutside(false)

Solution 5 - Android

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    AlertDialog.Builder(activity!!).apply {
        isCancelable = false
        setMessage("Your message")
        // your other adjustments
        return this.create()
    }
 }

worked for me.

The main thing is to use isCancelable = false over setCancellable(false)
within override fun onCreateDialog().

Solution 6 - Android

/**
 * Control whether the shown Dialog is cancelable.  Use this instead of
 * directly calling {@link Dialog#setCancelable(boolean)
 * Dialog.setCancelable(boolean)}, because DialogFragment needs to change
 * its behavior based on this.
 *
 * @param cancelable If true, the dialog is cancelable.  The default
 * is true.
 */
DialogFragment.setCancelable(boolean cancelable) {
    mCancelable = cancelable;
    if (mDialog != null) mDialog.setCancelable(cancelable);
}

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
QuestionArunView Question on Stackoverflow
Solution 1 - AndroidBlackbeltView Answer on Stackoverflow
Solution 2 - AndroidUsman KurdView Answer on Stackoverflow
Solution 3 - AndroidXenioneView Answer on Stackoverflow
Solution 4 - AndroidMirza AdilView Answer on Stackoverflow
Solution 5 - Androidivan8m8View Answer on Stackoverflow
Solution 6 - Androidxiaoyuan huView Answer on Stackoverflow