Alert Dialog

Toast
To interact with the user from the application to show messages sometimes require or ask how you want to happen proceed flow depending on a particular response or choice. For this type have several tools which can display messages popup information or require action.

Some examples are, and you will be using each in the most convenient.

The first option is the Toast messages that are above the screen no matter what application is running, but they have no interaction button, rather serves to show rapid, short and not so important.

Toast toast = Toast.makeText(getApplicationContext(), "Example de Message for Android", Toast.LENGTH_SHORT);
toast.show();

Alert Dialog
The next option is to display a dialog box with the OK button, they are seen only if the Activity is active on the screen but ensures that the user reads it and to press a button to continue the flow.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Example de Message for Android")
.setTitle("Attention!!")
.setCancelable(false)
.setNeutralButton("Accept",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

One option that is widely used is to ask the user whether to continue with an activity or not, with the typical buttons yes or no and in some cases with a third option that is often omitted or out. Depending on the option chosen by the user performs different action.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to continue with the theme?")
.setTitle("Warning")
.setCancelable(false)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setPositiveButton("Continue",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
TransferirDinero();
}
});
AlertDialog alert = builder.create();
alert.show();

List
It could also be a case where we need to show more than one option, type multichoise and responses are not dry. This can display a list of options.

final CharSequence[] items = {"Android OS", "iOS", "Windows Phone", "Meego"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("your preferred OS?");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast toast = Toast.makeText(getApplicationContext(), "you choise the option: " + items[item] , Toast.LENGTH_SHORT);
toast.show();
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

This option can be added a radioButtons.

final CharSequence[] items = {"Android OS", "iOS", "Windows Phone", "Meego"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("your preferred OS?");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast toast = Toast.makeText(getApplicationContext(), "you choise the option:: " + items[item] , Toast.LENGTH_SHORT);
toast.show();
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

One thought on “Alert Dialog

Leave a Reply

Your email address will not be published. Required fields are marked *