0

Hello Friends i made an alert dialog box that is fired when there is no net connection and when person clicks the ok button he would be directed to No Internet.class it is working fine but real problem is when user click in background of alert dialog box it automatically dismissed.How can i stop that?? I mean cancelling of dialog box .I also used this method SetCancelable(false); but it is not working.It gives an error.

Below is my Code

AlertDialog.Builder alert = new AlertDialog.Builder(this,R.style.AppCompatAlertDialogStyle);
        alert.setTitle("Internet Problem");
        alert.setMessage("No Internet!!!");

       // alert.SetCancelable(false);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                //Do something here where "ok" clicked and then perform intent from activity context
                Intent intent = new Intent(BaseActivity.this, NoInternet.class);
                BaseActivity.this.startActivity(intent);

            }
        });
  • 2
    Possible duplicate of [Prevent Android activity dialog from closing on outside touch](http://stackoverflow.com/questions/12102777/prevent-android-activity-dialog-from-closing-on-outside-touch). But be sure to read ALL of the answers. There are many which are upvoted higher than the accepted. – CubeJockey Jan 05 '16 at 14:21

3 Answers3

9

Just add alert.setCancelable(false);. The reason why it was not working (from the comment in code) is, you were using wrong method name before.

AlertDialog.Builder alert = new AlertDialog.Builder(...);

    alert.setCancelable(false); // not SetCancelable(false). Case sensitive

});
Msp
  • 2,493
  • 2
  • 20
  • 34
3
  Dialog dialog = new Dialog(context)
  dialog.setCanceledOnTouchOutside(false);
Ihor Bykov
  • 1,843
  • 3
  • 15
  • 22
2

Use setCancelable(false) method. Hope it helps.

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57