DialogFragment setCancelable property not working
Asked Answered
G

6

124

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;
    }
 }
Grecian answered 10/5, 2013 at 10:22 Comment(3)
instead of getDialog().setCancelable(false); you should call setCancelable(false);Angelo
if you click outside the boundry of dialog it must also be dismmised ?Athal
Are you trying in ice cream sandwich?Please refer to the link mentioned fantasypublishings.com/morePhpHelp/…Watchword
A
281
@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;
}
Angelo answered 10/5, 2013 at 10:46 Comment(5)
That's a truly incredible tip, THANKS. I wonder why, with say dialog.getWindow().requestFeature(--) you have to "include the dialog" on the getWindow?Assert
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 ;)Suint
In case you don't override the onCreateView, the setCancelable(false) can also be called from the public Dialog onCreateDialog(Bundle savedInstanceState)Foley
Not working for me. Dialog still gets dismiss on click of back button.Beatitude
@Angelo 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: #59825758Socialistic
A
64

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);
Athal answered 10/5, 2013 at 10:44 Comment(1)
This should be the accepted answer as it's applicable for both vanilla alert dialog fragments and custom dialog fragments.Foofaraw
P
35

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;
}
Pinsk answered 16/8, 2014 at 9:23 Comment(4)
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.Pinsk
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?Backer
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.Pinsk
Thanks! I was using alert.setCancelable(false) all this while and couldn't understand why it wouldn't work.Rizzo
V
2

Simple Solution in DialogFragment

Used

dialog.setCanceledOnTouchOutside(false)
Ventricular answered 31/3, 2020 at 10:26 Comment(0)
B
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().

Beaverboard answered 15/10, 2020 at 14:1 Comment(0)
N
-1
/**
 * 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);
}
Natelson answered 1/4, 2021 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.