Android Alert Dialog is one of the most important and basic component in Android applications. A Dialog is a small window that prompts the user to a decision or enters additional information.
Components
Title : Title of the alert dialog (Optional)
Message : This displays the message to the user. It can be a string message
Action Buttons : Represents a push-button widget. Push-buttons can be pressed, or clicked, by the user to perform an action.It classified into three types,they are listed below
Positive : The user wants to accept the action.It is normally displayed as OK/YES.
Negative : The user wants to cancel the action.It is normally displayed as CANCEL/NO.
Neutral : The user wants to postpone the decision.It is normally displayed as LATER
For displaying an alert dialog, first, you need to create an object of ” AlertDialogBuilder”. Only using this object, you can set the characteristics for the dialog like the theme, title, message as well as the action buttons caption and it’s callback actions.
To create an object of ” AlertDialogBuilder “ pass the current activity as the argument for the constructor (must pass the activity using this ( ClassName.this) in Activity / getActivity() in fragment) . To set theme for the alert dialog use the following syntax:
// Instantiate an AlertDialog.Builder with its constructor | |
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); | |
AlertDialog.Builder alertDialogBuilder1 = new AlertDialog.Builder(new ContextThemeWrapper(this, | |
android.R.style.Theme_DeviceDefault_Light_Dialog)); | |
// You can replace your own theme by replacing the themeID | |
AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(this,themeID); |
To set the title, message, icon and cancelable type for the alert dialog,
//set the title to be appear in the dialog | |
alertDialogBuilder.setTitle("Draft"); | |
//sets the message to be displayed in the alert dialog | |
alertDialogBuilder.setMessage("Discard draft?"); | |
//sets the property that the dialog can be cancelled or not | |
alertDialogBuilder.setCancelable(false); | |
// set the icon of the alert dialog box | |
alertDialogBuilder.setIcon(android.R.drawable.ic_dialog_alert); |
For creating an alert dialog with action buttons use the following snippet,
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); | |
alertDialogBuilder.setTitle("Draft"); | |
alertDialogBuilder.setMessage("Discard draft?"); | |
alertDialogBuilder.setCancelable(false); | |
alertDialogBuilder.setIcon(android.R.drawable.ic_dialog_alert); | |
alertDialogBuilder.setPositiveButton("DISCARD", new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
// continue with discard | |
Toast.makeText(AlertDialogActivity.this, "Discard", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
alertDialogBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
// do nothing | |
Toast.makeText(AlertDialogActivity.this, "Cancel", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
alertDialogBuilder.show(); |
Don’t forget to call ” show “, because the show is the function used to display the alert dialog.
Jaison Fernando
Latest posts by Jaison Fernando (see all)
- Phone number auth using Firebase Authentication SDK - March 20, 2020
- Password-less email auth using Firebase Authentication SDK - March 9, 2020
- How to use SharedPreferences API in Android? - February 10, 2020