6

I am using alert dialog with following properties.The problem is the dialog is dismiissed when i click back button or touch outside the dialog i don't want that.How can i solve this because setCancelable is not helping.

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.check_internet);
    builder.setCancelable(false);
Ravi
  • 4,872
  • 8
  • 35
  • 46

6 Answers6

18

Since you are extending a DialogFragment you need to call

setCancelable(false);

inside its onCreate

only setCancelable(false);

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
2
AlertDialog.Builder builder = new AlertDialog.Builder(this); // this activity context
builder.setMessage(R.string.check_internet);
AlertDialog alertDialog =builder.create();
alertDialog.setCancelable(false);
alertDialog.show();
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Vipin Sahu
  • 1,441
  • 1
  • 18
  • 29
0
new AlertDialog.Builder(getActivity());

Try to put an activity there, which is in the bottom of stack(not the top)

0

This is the default behavior for all the Android devices, think twice before changing that because user expects it when he presses the back button.

However you can change that behavior by overriding OnBackPressed() method of the Dialog/DialogFragment class. Don't call super.OnBackPressed() inside the overridden function however that may leak memory so be careful..

Amit
  • 13,134
  • 17
  • 77
  • 148
0
   public void showAlert1(){
     MainActivity.this.runOnUiThread(new Runnable() {
     public void run() {
     AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setMessage(R.string.check_internet); 
                    builder.setIcon(R.drawable.tick);
                    builder.setCancelable(false);
                    AlertDialog alert = builder.create();
                    alert.show();              
                }
            });
           }
Make it Simple
  • 1,832
  • 5
  • 32
  • 57
0

No that is the default behaviour of the alert dialog see this link dialog

vinoth
  • 485
  • 4
  • 16
  • One note about that comment. You can implement a onDismiss, however it seems to require API 17 as a min. – Shygar May 25 '14 at 01:41