Simple Exit dialog box in android studio with code in java

 Step 1- In  MainActivity.java file override an method onBackPressed()

Write this code in this method

@Override
public void onBackPressed() {
//--------> Create a dialog for Show dialog after click backed --->//
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit ?")
.setCancelable(false)
//--------> Code for Exit from app --->//
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

MainActivity.super.onBackPressed();
}
//--------> Code for cancel --->//
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

dialog.cancel();
}
});
builder.create();
builder.show();
}


                              <<<<<=============ScreenShot============>>>>>>>>>





Post a Comment

Previous Post Next Post