You can inform the user about the different actions he can take using the AlertDialog class.This class allows the user to take three different actions which are Positive action,Neutral action ,negative action.
These actions are set using the different methods of the AlertDialog class.The different actions which user can take using the AlertDialog are:
- User can take Positive action which means user wants to execute the corresponding action.Call the setPositiveButton() method in this scenario.
- User can take Neutral action which means that user wants to postpone the action..Call the setNeutralButton() method in this scenario.
- User can take the Negative action.Call the setNegativeButton() in this scenario.
The methods used to indicate the various actions taken by the user such as positive action or negative action are defined by the AlertDialog.Builder class.The Builder class is a nested class defined in the AlertDialog class.
For showing dialog you need to create an AlertDialog object.Once you create this object you need to call the different methods corresponding to the different positive,negative and neutral scenarios.
To create alert dialog
Create an object of type AlertDialog
Call the different methods of the AlertDialog object.To set the title call the setTitle() method.Similarly call the setMessage() method to set the message.
Provide the callbacks for:
- Positive action
- Neutral action
- Ngative action
alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setTitle("title"); alertDialogBuilder.setMessage("message") alertDialogBuilder setPositiveButton("positivemessage", new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int action) { // } }); alertDialogBuilder .setNeutralButton(R.string.discard_changes, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int action) { // } }) alertDialogBuilder .setNegativeButton(android.R.string.cancel_dialog, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int action) { // } }) .create(); alertDialog.show();
Leave a Reply