A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed. Show Dialog Design For information about how to design your dialogs, including recommendations for language, read the Dialogs design guide. The AlertDialog A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.DatePickerDialog or TimePickerDialog A dialog with a pre-defined UI that allows the user to select a date or time.
Caution: Android includes another dialog class called These classes define the style and structure for your dialog, but you should use a Using The following sections in this guide describe how to use a Note: Because the Creating a Dialog FragmentYou can accomplish a wide variety of dialog designs—including custom layouts and those described in the
Dialogs design guide—by extending For example, here's a basic
Figure 1. A dialog with a message and two action buttons. Now, when you create an instance of this class and call The next section describes more about using the Depending on how complex your dialog is, you can implement a variety of other callback methods in the
Building an Alert DialogThe Figure 2. The layout of a dialog.
The To build an
The following topics show how to define various dialog attributes using the Adding buttonsTo add action buttons like those in figure 2, call the
The There are three different action buttons you can add: PositiveYou should use this to accept and continue with the action (the "OK" action).NegativeYou should use this to cancel the action.NeutralYou should use this when the user may not want to proceed with the action, but doesn't necessarily want to cancel. It appears between the positive and negative buttons. For example, the action might be "Remind me later."You can add only one of each button type to an Figure 3. A dialog with a title and list. Adding a listThere are three kinds of lists available with the
To create a single-choice list like the one in figure 3, use the
Because the list appears in the dialog's content area, the dialog cannot show both a message and a list and you should set a title for the dialog with If you choose to back your list with a Note: By default, touching a list item dismisses the dialog, unless you're using one of the following persistent choice lists. Adding a persistent multiple-choice or single-choice listTo add a list of multiple-choice items (checkboxes) or single-choice items (radio buttons), use the Figure 4. A list of multiple-choice items. For example, here's how you can create a multiple-choice list like the one shown
in figure 4 that saves the selected items in an
Although both a traditional list and a list with radio buttons provide a "single choice" action, you should use Creating a Custom LayoutFigure 5. A custom dialog layout. If you want a custom layout in a dialog, create a layout and add it to an By default, the custom layout fills the dialog window, but you can still use For example, here's the layout file for the dialog in Figure 5: res/layout/dialog_signin.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:src="@drawable/header_logo" android:layout_width="match_parent" android:layout_height="64dp" android:scaleType="center" android:background="#FFFFBB33" android:contentDescription="@string/app_name" /> <EditText android:id="@+id/username" android:inputType="textEmailAddress" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_marginBottom="4dp" android:hint="@string/username" /> <EditText android:id="@+id/password" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_marginBottom="16dp" android:fontFamily="sans-serif" android:hint="@string/password"/> </LinearLayout> Tip: By default, when you set an To inflate the layout in your
Tip: If you want a custom dialog, you can instead display an <activity android:theme="@android:style/Theme.Holo.Dialog" > That's it. The activity now displays in a dialog window instead of fullscreen. Passing Events Back to the Dialog's HostWhen the user touches one of the dialog's action buttons or selects an item from its list, your For example, here's a
The activity hosting the dialog creates an instance of the dialog with the dialog fragment's constructor and receives the dialog's events through an implementation of the
Because the host activity implements the
Showing a DialogWhen you want to show your dialog, create an instance of your You can get the
The second argument, Showing a Dialog Fullscreen or as an Embedded FragmentYou might have a UI design in which you want a piece of the UI to appear as a dialog in some situations, but as a full screen or embedded fragment in others (perhaps depending on whether the device is a large screen or small screen). The However,
you cannot use Here's an example
And here's some code that decides whether to show the fragment as a dialog or a fullscreen UI, based on the screen size:
For more information about performing fragment transactions, see the Fragments guide. In this example, the res/values/bools.xml <!-- Default boolean values --> <resources> <bool name="large_layout">false</bool> </resources> res/values-large/bools.xml <!-- Large screen boolean values --> <resources> <bool name="large_layout">true</bool> </resources> Then you can initialize the
Showing an activity as a dialog on large screensInstead of showing a dialog as a fullscreen UI when on small screens, you can accomplish the same result by showing an To show an activity as a
dialog only when on large screens, apply the <activity android:theme="@android:style/Theme.Holo.DialogWhenLarge" > For more information about styling your activities with themes, see the Styles and Themes guide. Dismissing a DialogWhen the user touches any
of the action buttons created with an The system also dismisses the dialog when the user touches an item in a dialog list, except when the list uses radio buttons or checkboxes. Otherwise, you can manually dismiss your dialog by calling In case you need to perform certain actions when the dialog goes away, you can implement the You can also cancel a dialog.
This is a special event that indicates the user explicitly left the dialog without completing the task. This occurs if the user presses the Back button, touches the screen outside the dialog area, or if you explicitly call As shown in the example above, you can respond to the cancel event by implementing Note: The system calls |