Sagunesh Grover
Sagunesh Grover

Reputation: 41

How to prevent dismiss of alert dialog box when user click in background of it?

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

            }
        });

Upvotes: 0

Views: 4842

Answers (3)

Use setCancelable(false) method. Hope it helps.

Upvotes: 2

Msp
Msp

Reputation: 2493

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

});

Upvotes: 9

Ihor Bykov
Ihor Bykov

Reputation: 1938

  Dialog dialog = new Dialog(context)
  dialog.setCanceledOnTouchOutside(false);

Upvotes: 3

Related Questions