Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog's setCancelable(false) method not working

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.

like image 248
lupchiazoem Avatar asked Jan 18 '12 06:01

lupchiazoem


People also ask

What is setCancelable?

setCancelable(false) means that back key doesn't close the dialog. If for example you want the dialog to be closed only if the user click some dialog button then both setCanceledOnTouchOutside() and setCancelable() should be set to false.

What is setCanceledOnTouchOutside?

setCanceledOnTouchOutside only prevents dismissing by clicking outside of the dialog. But you can still dismiss it with back button for instance. If you don't want your dialog to be cancelable at all use dialog.setCancelable(false)

What is Alertdialog builder in Android?

Alert Dialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button.


1 Answers

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"); } 
like image 107
Sandy Avatar answered Oct 15 '22 01:10

Sandy