Alert Dialog
There are three regions in an alert dialog. These are:
- Title
This is optional and it should be generally used when the content area is occupied by a detailed message, a list, or a custom layout. If you need to state a simple message or question, you don't need a title.
- Content Area
This area is used to display any message, a list, or other custom layout.
- Action Buttons
Alert Dialog allows maximum three action buttons in a dialog.
To build an AlertDialog, you need AlertDialog.Builder class that allow you to create and build an AlertDialog of specific characteristics.
Building an AlertDialog
//1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//2. Use various methods of AlertDialog.Builder class to set the dialog characteristics
//Set the message you want to show
builder.setMessage("I Love StudyTonight");
//Set the title of an AlertDialog
builder.setTitle("Learning Dialog");
//3. Create an AlertDialog with the above characteristics and display it.
//Creating an AlertDialog
AlertDialog dialog = builder.create();
//Displaying it
dialog.show();
Adding Action Buttons
There are three kinds of buttons in AlertDialog:
- Positive Button
This is an affirmative button that is used to proceed further. For example, OK button.
- Negative Button
This is a dismissive button that is used to cancel an action. For example, Cancel Button.
- Neutral Button
This button is use when the user may not want to proceed with any action, but doesn't necessarily want to cancel it. For example, "Remind me later" Button
Note: You can add only one button of each type to an AlertDialog. That is, you cannot have more than one positive, negative and neutral button.
To add the action buttons, we need to call the setPositiveButton()
, setNegativeButton()
and setNeutralButton()
methods as follows:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//Adding positive button
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(this,"OK button was clicked",Toast.LENGTH_LONG).show();
}
});
//Adding negative button
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(this,"Cancel button was clicked",Toast.LENGTH_LONG).show();
}
});
//Adding neutral button
builder.setNeutralButton("Remind Me later", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(this,"Remind me later button was clicked",Toast.LENGTH_LONG).show();
} });
The setPositiveButton()
, setNegativeButton()
and setNeutralButton()
methods has two parameters:
- Name/Title
This parameter is used to set the name/title of the button.
- OnclickListener instance
DialogInterface.OnClickListener
interface is used that defines an action to be performed whenever the user presses that specific button. This interface overrides onClick()
method which is called whenever the user clicks the button.