UI Dialog Elements
- Introduction
- A dialog is usually a small window that appears in front of the current activity.
The activity loses focus and the dialog accepts all user interaction.
- Dialogs are normally used for notifications that should interupt the user and to perform
short tasks that directly relate to the application in progress.
- A dialog is hosted in a DialogFragment, floating on top of its activity's
window.
The dialog is controlled by the fragment.
- A
fragment is a portion of an Activity's user interface.
- Multiple fragments can be put into the UI, along with "regular" UI elements.
- Fragments have both UI elements and code - kinda "subactivities".
They have regular lifecycles.
- Fragments can be reused by multiple activities in an app.
- Dialogs are raised with the show method, and are initialized in the fragment's
onCreateDialog method that is called the first time the dialog is displayed.
- System dialogs are available for standard tasks, e.g., confirm an action.
- Custom dialogs can be created in the onCreateView method of the fragment.
- Dialog fragments can define interfaces for the activity to implement for communication.
- A dialog can be modified each time it is displayed in the fragment callback method
onStart.
- A dialog fragment can dismiss itself using the dismiss method.
- Standard AlertDialogs
- A dialog that can manage zero, one, two, or three buttons, and/or a list of selectable
items that can include checkboxes or radio buttons.
- The AlertDialog.Builder class is useful for building AlertDialogs.
Useful methods include
- setMessage sets the message at the top of a dialog with buttons.
- setPositiveButton and setNegativeButton - set the text and
listener for the "positive" and "negative" buttons.
- setCancelable - to enable or disable the phone's back button (disabling
forces the user to choose).
- setTitle - sets the title for a list dialog.
- setItems - sets the items of a selection list dialog.
- setSingleChoiceItems - sets the items of a radio button list dialog.
- setMultiChoiceItems - sets the items of a checkbox list dialog.
- A DialogInterface.OnClickListener is used to listen for clicks on a dialog
(except for checkbox lists - see next).
Implement the onClick method of the interface.
- A DialogInterface.OnMultiChoiceClickListener is used to listen for clicks on
a checkbox list dialog.
- Example
MainActivity.java
activity_main.xml
styles.xml
dimens.xml
UIDialogsFragment.java
strings.xml
- DatePickerDialog and TimePickerDialog
- Custom dialogs