Working with Dialogs

Handset screens are small, and user interface real estate is valuable. Sometimes you want to handle a small amount of user interaction without creating an entirely new activity. In such instances, creating an activity dialog can be very handy. Dialogs can be helpful for creating very simple user interfaces that do not necessitate an entirely new screen or activity to function. Instead, the calling activity dispatches a dialog, which can have its own layout and user interface, with buttons and input controls.

Table 3.2 lists the important methods for creating and managing activity dialog windows.

TABLE 3.2 Important Dialog Methods of the Activity Class





Shows a dialog, creating it if necessary.



Is a callback when a dialog is being created for the first time and added to the activity dialog pool.



Is a callback for updating a dialog on-the-fly. Dialogs are created once and can be used many times by an activity. This callback enables the dialog to be updated just before it is shown for each showDialog() call.



Dismisses a dialog and returns to the activity. The dialog is still available to be used again by calling showDialog() again.



Removes the dialog completely from the activity dialog pool.

Activity classes can include more than one dialog, and each dialog can be created and then used multiple times.

There are quite a few types of ready-made dialog types available for use in addition to the basic dialog. These are AlertDialog, CharacterPickerDialog, DatePickerDialog, ProgressDialog, and TimePickerDialog.

You can also create an entirely custom dialog by designing an XML layout file and using the Dialog.setContentView() method. To retrieve controls from the dialog layout, you simply use the Dialog.findViewById() method.

0 0

Post a comment