Working with Dialogs Android

An Activity can use dialogs to organize information and react to user-driven events. For example, an activity might display a dialog informing the user of a problem or ask the user to confirm an action such as deleting a data record. Using dialogs for simple tasks helps keep the number of application activities manageable.

Exploring the Different Types of Dialogs

There are a number of different dialog types available within the Android SDK. Each has a special function that most users should be somewhat familiar with. The dialog types available include

  • Dialog: The basic class for all Dialog types. A basic Dialog is shown in the top left of Figure.
  • AlertDialog: A Dialog with one, two, or three Button controls. An AlertDialog is shown in the top center of Figure.
  • CharacterPickerDialog: A Dialog for choosing an accented character associated with a base character.A CharacterPickerDialog is shown in the top right of Figure.
  • DatePickerDialog: A Dialog with a DatePicker control. A DatePickerDialog is shown in the bottom left of Figure.
  • ProgressDialog: A Dialog with a determinate or indeterminate ProgressBar control. An indeterminate ProgressDialog is shown in the bottom center of Figure.
  • TimePickerDialog: A Dialog with a TimePicker control. A TimePickerDialog is shown in the bottom right of Figure.

The different dialog types available in Android.

The different dialog types available in Android

If none of the existing Dialog types is adequate, you can also create custom Dialog windows, with your specific layout requirements.

Tracing the Lifecycle of a Dialog

Each Dialog must be defined within the Activity in which it is used. A Dialog may be launched once, or used repeatedly. Understanding how an Activity manages the Dialog lifecycle is important to implementing a Dialog correctly. Let’s look at the key methods that an Activity must use to manage a Dialog:

  • The showDialog() method is used to display a Dialog.
  • The dismissDialog() method is used to stop showing a Dialog. The Dialog is kept around in the Activity’s Dialog pool. If the Dialog is shown again using showDialog(), the cached version is displayed once more.
  • The removeDialog() method is used to remove a Dialog from the Activity objects Dialog pool.The Dialog is no longer kept around for future use. If you call showDialog() again, the Dialog must be re-created.

Adding the Dialog to an Activity involves several steps:

  1. Define a unique identifier for the Dialog within the Activity.
  2. Implement the onCreateDialog() method of the Activity to return a Dialog of the appropriate type, when supplied the unique identifier.
  3. Implement the onPrepareDialog() method of the Activity to initialize the Dialog as appropriate.
  4. Launch the Dialog using the showDialog() method with the unique identifier.

Defining a Dialog
A Dialog used by an Activity must be defined in advance. Each Dialog has a special identifier (an integer).When the showDialog() method is called, you pass in this identifier. At this point, the onCreateDialog() method is called and must return a Dialog of the appropriate type.

It is up to the developer to override the onCreateDialog() method of the Activity and return the appropriate Dialog for a given identifier. If an Activity has multiple Dialog windows, the onCreateDialog() method generally contains a switch statement to return the appropriate Dialog based on the incoming parameter—the Dialog identifier.

Initializing a Dialog
Because a Dialog is often kept around by the Activity in its Dialog pool, it might be important to re-initialize a Dialog each time it is shown, instead of just when it is created the first time. For this purpose, you can override the onPrepareDialog() method of the Activity.

Although the onCreateDialog() method may only be called once for initial Dialog creation, the onPrepareDialog() method is called each time the showDialog() method is called, giving the Activity a chance to modify the Dialog before it is shown to the user.

Launching a Dialog
You can display any Dialog defined within an Activity by calling its showDialog() method and passing it a valid Dialog identifier—one that will be recognized by the< onCreateDialog() method.

Dismissing a Dialog
Most types of dialogs have automatic dismissal circumstances. However, if you want to force a Dialog to be dismissed, simply call the dismissDialog() method and pass in the Dialog identifier.

Removing a Dialog from Use
Dismissing a Dialog does not destroy it. If the Dialog is shown again, its cached contents are redisplayed. If you want to force an Activity to remove a Dialog from its pool and not use it again, you can call the removeDialog() method, passing in the valid Dialog identifier.

Working with Custom Dialogs
When the dialog types do not suit your purpose exactly, you can create a custom Dialog. One easy way to create a custom Dialog is to begin with an AlertDialog and use an AlertDialog.Builder class to override its default layout. In order to create a custom Dialog this way, the following steps must be performed:

  1. Design a custom layout resource to display in the AlertDialog.
  2. Define the custom Dialog identifier in the Activity.
  3. Update the Activity’s onCreateDialog() method to build and return the appropriate custom AlertDialog.You should use a LayoutInflater to inflate the custom layout resource for the Dialog.
  4. Launch the Dialog using the showDialog() method.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Android Topics