Creating An Alert Dialog for Exiting the app (Exit Confirmation)
This topic is a part of My Complete Andorid Course
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.warning)
.setTitle("Exit !!")
.setMessage("Do you really want to Exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setNeutralButton("Help", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Press Yes to Exit", Toast.LENGTH_SHORT).show();
}
})
.show();
}
});
//back button is pressed
@Override
public void onBackPressed() {
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.warning)
.setTitle("Exit !!")
.setMessage("Do you really want to Exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setNeutralButton("Help", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Press Yes to Exit", Toast.LENGTH_SHORT).show();
}
})
.show();
}