Example: Show Alert Dialog in Android



1. Introducing Android Alert Dialog

Dialog boxes are a common UI metaphor in desktop and web applications. They 're used to help users answer questions, make selections, confirm actions, and read warning or error messages. An Android Dialog is a floating window that partially obscures the Activity that launched it.
Creating alert dialog is very easy. In this tutorial i will be discussing about creating different alert dialogues with one button(ok button), two buttons(yes or no buttons) and three buttons(yes, no and cancel buttons).


2. Showing Alert Dialog
2.1. Android alert dialog with One Button

The following code will create a simple alert dialog with one button. In the following code setTitle() method is used for set Title to alert dialog. setMessage() is used for setting message to alert dialog. setIcon() is to set icon to alert dialog.





Code:
// Creating alert Dialog with one Button

AlertDialog alertDialog1 = new AlertDialog.Builder(
AlertDialogActivity.this).create();

// Setting Dialog Title
alertDialog1.setTitle("Alert Dialog");

// Setting Dialog Message
alertDialog1.setMessage("Welcome to 9Android.net");

// Setting Icon to Dialog
alertDialog1.setIcon(R.drawable.tick);

// Setting OK Button
alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
// closed
Toast.makeText(getApplicationContext(),
"You clicked on OK", Toast.LENGTH_SHORT).show();
}
});

// Showing Alert Message
alertDialog1.show();



2.2. Android alert dialog with Two Button

The following code will create alert dialog with two button. setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog.





Code:
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(
AlertDialogActivity.this);

// Setting Dialog Title
alertDialog2.setTitle("Confirm Delete...");

// Setting Dialog Message
alertDialog2.setMessage("Are you sure you want delete this file?");

// Setting Icon to Dialog
alertDialog2.setIcon(R.drawable.delete);

// Setting Positive "Yes" Btn
alertDialog2.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(),
"You clicked on YES", Toast.LENGTH_SHORT)
.show();
}
});
// Setting Negative "NO" Btn
alertDialog2.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(),
"You clicked on NO", Toast.LENGTH_SHORT)
.show();
dialog.cancel();
}
});

// Showing Alert Dialog
alertDialog2.show();



2.3. Android alert dialog with Three Button

Here setNeutralButton() is used to create a neutral cancel button.





Code:
// Creating alert Dialog with three Buttons

AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(
AlertDialogActivity.this);

// Setting Dialog Title
alertDialog3.setTitle("Save File...");

// Setting Dialog Message
alertDialog3.setMessage("Do you want to save this file?");

// Setting Icon to Dialog
alertDialog3.setIcon(R.drawable.save);

// Setting Positive Yes Button
alertDialog3.setPositiveButton("YES",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// User pressed Cancel button. Write Logic Here
Toast.makeText(getApplicationContext(),
"You clicked on YES", Toast.LENGTH_SHORT)
.show();
}
});
// Setting Positive Yes Btn
alertDialog3.setNeutralButton("NO",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// User pressed No button. Write Logic Here
Toast.makeText(getApplicationContext(),
"You clicked on NO", Toast.LENGTH_SHORT)
.show();
}
});
// Setting Positive "Cancel" Btn
alertDialog3.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// User pressed Cancel button. Write Logic Here
Toast.makeText(getApplicationContext(),
"You clicked on Cancel", Toast.LENGTH_SHORT)
.show();
}
});
// Showing Alert Dialog
alertDialog3.show();
break;

Download source code : https://www.dl.9android.net/index.php?act=dl&id=1365169978
Source : https://www.9android.net/show-alert-dialog/

Published April 8, 2013