Anish
Anish

Reputation: 166

how to stop alertdialog from automatic disappearing

Below is my code to alert the user whenever GPS is off. Here my user has to click on one of the options and move further. But if a user clicks elsewhere on screen other than this alert, this alert disappears. How can I stop this alert dialog from disappearing automatically. help please

Builder bd = new AlertDialog.Builder(myTest.this);
    bd.setTitle("Alert");       
    bd.setMessage("GPS is disabled. Do you want to");
    bd.setNegativeButton("Enable GPS", new DialogInterface.OnClickListener() {              
                public void onClick(DialogInterface dialog, int which) {
                    startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                }});            
            bd.setPositiveButton("Close App", new DialogInterface.OnClickListener() {               
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
                });
    bd.show();  

Upvotes: 3

Views: 2254

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93561

Use bd.setCancelable(false). This will make it so it can only go away by pressing the buttons.

Upvotes: 6

Related Questions