123

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;
    }
 }
Arun PS
  • 4,610
  • 6
  • 41
  • 56
  • 34
    instead of getDialog().setCancelable(false); you should call setCancelable(false); – Blackbelt May 10 '13 at 10:34
  • if you click outside the boundry of dialog it must also be dismmised ? – Usman Kurd May 10 '13 at 10:35
  • Are you trying in ice cream sandwich?Please refer to the link mentioned http://www.fantasypublishings.com/morePhpHelp/ice_cream_sandwich__android_40_Dialog_gets_canceled_when_touched_outside_of_dialog_window__Stack_Overflow_page93701.php – Remmyabhavan May 10 '13 at 10:45

6 Answers6

280
@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;
}
Marko
  • 20,385
  • 13
  • 48
  • 64
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • That's a truly incredible tip, THANKS. I wonder why, with say *dialog.getWindow().requestFeature(--)* you have to "include the dialog" on the getWindow? – Fattie Jun 05 '14 at 07:42
  • 7
    It's not a tip. A fragment is wrapping your dialog, it's normal that you have to deal with the fragment instead of the dialog itself ;) – andrea.rinaldi May 13 '15 at 15:07
  • 1
    In case you don't override the onCreateView, the setCancelable(false) can also be called from the public Dialog onCreateDialog(Bundle savedInstanceState) – user2924714 Nov 15 '15 at 08:57
  • 2
    Not working for me. Dialog still gets dismiss on click of back button. – Pinkesh Darji May 04 '18 at 10:39
  • @Blackbelt I have a similar use case but in my case a touch outside the dialog is not dismissing the dialog. I am using a DatePicker within the DialogFragment. What would I use for "R.layout.dialog_test" in your answer above? My full question is listed here: https://stackoverflow.com/questions/59825258/android-how-to-dismiss-a-datepicker-dialogfragment-when-touching-outside/59825354?noredirect=1#comment105791101_59825354 – AJW Jan 21 '20 at 21:05
63

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);
Marko
  • 20,385
  • 13
  • 48
  • 64
Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
  • 1
    This should be the accepted answer as it's applicable for both vanilla alert dialog fragments and custom dialog fragments. – Ganesh Mohan May 15 '17 at 21:44
32

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;
}
Marko
  • 20,385
  • 13
  • 48
  • 64
Xenione
  • 2,174
  • 1
  • 23
  • 30
  • Yes I try above solution, even the checked one but not works for me, that answer is new so need time to be upvoted, Thanks anyway. – Xenione Aug 27 '14 at 08:04
  • Well this is true, even if I think this behaviour from Android's side is quite strange, as you explicitly use for example AlertDialog.Builder to build up your dialog, you would think that those attributes overrides the subclasses. But I maybe missing something here? – Robert Sep 26 '14 at 21:04
  • I think dialog behaviur after be wrapped in a dialogfragment not responde to the AlertDialog anymore or at leat at the bigining. I think all goes throught fragmentDialog instead. – Xenione Sep 27 '14 at 15:42
  • Thanks! I was using alert.setCancelable(false) all this while and couldn't understand why it wouldn't work. – Aldo May 27 '18 at 21:41
2

Simple Solution in DialogFragment

Used

dialog.setCanceledOnTouchOutside(false)
Mirza Adil
  • 352
  • 5
  • 9
1
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().

ivan8m8
  • 388
  • 4
  • 17
0
/**
 * 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);
}
xiaoyuan hu
  • 175
  • 2
  • 2