Dialog

Dialog boxes are a common UI metaphor in desktop, web, and mobile applications. They’re used to help users answer questions, make selections, and confirm actions, and to display warning or error  messages. Dialog boxes in Android are partially transparent, floating Activities that partially obscure the Activities that launched them.

There are three ways to implement a dialog in Android: .

- Using the Dialog class: As well as the general-purpose AlertDialog class, Android includes a number of specialist classes that extend Dialog. Each is designed to provide specific dialog box functionality. A Dialog-class-based screen is constructed entirely within its calling Activity, so it doesn’t need to be registered in the manifest as its life cycle is controlled entirely by the calling Activity.

- Dialog-themed Activities: You can apply the dialog theme to a regular Activity to give it the appearance of a standard dialog box.

- Toasts: Toasts are special non-modal transient message boxes, often used by Broadcast Receivers and Services to notify users of events occurring in the background.

The Alert Dialog Class
The AlertDialog class is one of the most versatile Dialog-class implementations. It offers a number of
options that let you construct screens for some of the most common dialog-box use cases, including:

- Presenting a message to the user offering them one to three options in the form of buttons. This functionality is probably familiar to you if you’ve done any desktop programming for which the buttons presented are usually a combination of OK, Cancel, Yes, and No.
- Offering a list of options in the form of checkboxes or radio buttons.
- Providing a text entry box for user input.

To construct the Alert Dialog user interface, create a new AlertDialog.Builder object as follows:
AlertDialog.Builder ad = new AlertDialog.Builder(context);

You can then assign values for the title and message to display, and optionally assign values to be used
for any buttons, selection items, and text input boxes you wish to display. That includes setting event
listeners to handle user interaction.

Specialist Input Dialogs
One of the major uses of dialog boxes is to provide an interface for user input. Android includes several
specialist dialog boxes that encapsulate controls designed to facilitate common user-input requests.

They include the following:
- CharacterPickerDialog: Lets users select an accented character based on a regular character source.
- DatePickerDialog: Lets users select a date from a DatePicker View. The constructor includes a callback listener to alert your calling Activity when the date has been set.
-TimePickerDialog: Similar to the Date Picker Dialog, this dialog lets users select a time from a TimePicker View.
- ProgressDialog: A dialog that displays a progress bar beneath a message text box. Perfect for keeping the user informed of ongoing progress of a time-consuming operation.

Configuring an Alert Dialog
Context context = MyActivity.this;
String title = "It is Pitch Black";
String message = "You are likely to be eaten by a grue.";
String button1String = "Go Back";
String button2String = "Move Forward";
AlertDialog.Builder ad = new AlertDialog.Builder(context);
ad.setTitle(title);
ad.setMessage(message);
ad.setPositiveButton(button1String,new OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
eatenByGrue();
} });
ad.setNegativeButton(button2String,
new OnClickListener(){
public void onClick(DialogInterface dialog, int arg1) {
// do nothing
}
});
ad.setCancelable(true);
ad.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
eatenByGrue();
}
});

To display an Alert Dialog that you’ve created callshow:
ad.show();

A better alternative is using your Activity’sonCreateDialog and onPrepareDialog handlers to create dialog instances that can persist state.

No comments:

Post a Comment