82

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.

lupchiazoem
  • 8,026
  • 6
  • 36
  • 42

9 Answers9

150

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");
}
Sandeep Kumar P K
  • 7,412
  • 6
  • 36
  • 40
  • Yes, it did help. Thanks much for your answer. Actually, I was drafting a response and meanwhile you had posted yours. – lupchiazoem Jan 19 '12 at 06:03
  • 19
    All - As it might help you so, providing this info - `setCancelable(false)` does not work in above code because `AlertDialog` creation is part of `DialogFragment` class. Please click 'this' link in above answer which points to full code. From `DialogFragment` class description - "This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.". – lupchiazoem Jan 19 '12 at 06:05
  • 1
    Link is broken. Try to phrase an answer that is not dependent on external sources – Nilzor Apr 12 '20 at 08:20
16

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

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Green
  • 161
  • 1
  • 2
  • If you want your dialog to be cancellable, by clicking outside it, but also want to be notified that it was cancelled, setCancelable (true) and override the Dialogs cancel method. @Override public void cancel() { – pstorli May 24 '18 at 18:08
12
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.

Jon
  • 9,156
  • 9
  • 56
  • 73
DoruChidean
  • 7,941
  • 1
  • 29
  • 33
10

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

Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
2

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;
2

In Kotlin for making dialog non-dismissible

dialog.isCancelable = false
Boken
  • 4,825
  • 10
  • 32
  • 42
pratham kesarkar
  • 3,770
  • 3
  • 19
  • 29
2

In DialogFragment

Used:-

dialog.setCanceledOnTouchOutside(false)

Mirza Adil
  • 352
  • 5
  • 9
1

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);
Mohsen Emami
  • 2,709
  • 3
  • 33
  • 40
0
        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();
            });
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 31 '21 at 00:14