Android Apps for Absolute Beginners

Page 151

140

CHAPTER 7: UI Design: Buttons, Menus, and Dialogs

Adding Dialogs An Android dialog is always created as part of an activity, and is presented in the form of a small, gray pop-up window that appears on top of the current activity’s UI. Android dims that UI so that it does not compete with the dialog box. The Dialog class is used to create an interruption of your current activity in order to collect or relay information to your application’s end user. Examples of uses for dialogs include alert notifications, end-user option selection, information data collection, date selection, time selection, task or processing progress bar monitoring, and so on.

Using Custom Dialog Subclasses Four custom subclasses of the Dialog class are provided as part of the Android API:

AlertDialog

ProgressDialog

DatePickerDialog

TimePickerDialog

You can also subclass your own custom Dialog class (say, CustomDialog) so that it does exactly what you need it to do. The general way to create a basic dialog within any given activity is via the onCreateDialog(int) method. Android uses this method to track the dialog created, which activity it belongs to, and its current state. To display a dialog once it is created, you use the showDialog(int) method, specifying the number of the dialog you wish to display. To hide or dismiss a Dialog object, use the dismissDialog(int) method, and the Dialog object will be removed from memory and the application. Here, we’ll take a closer look at the most often used (and the recommended) Dialog class: AlertDialog. Android provides an easy and powerful way to construct alert dialogs with many features.

Displaying an Alert Dialog The AlertDialog class provides a lot of built-in dialog features, such as a title, user message, up to three buttons, and a list of selectable items. You can even use check boxes and radio buttons in your list. The AlertDialog works its magic via a dialog builder that provides a ready-made dialog code structure for you to create complicated dialogs via the AlertDialog.Builder class. As shown in the boxed areas of Figure 7–21, there are four main parts to adding our AlertDialog to our existing Android application.

www.it-ebooks.info


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.