|
1 |
| -# Dialog |
| 1 | +<!--docs: |
| 2 | +title: "Alert Dialogs" |
| 3 | +layout: detail |
| 4 | +section: components |
| 5 | +excerpt: "Alert Dialogs are modal windows that must be interacted." |
| 6 | +iconId: dialog |
| 7 | +path: /catalog/dialog/ |
| 8 | +--> |
| 9 | + |
| 10 | +# Alert Dialogs |
| 11 | + |
| 12 | +An `AlertDialog` is a window that interrupts the current user flow. It is used to flag important choices like discarding drafts or changing permission settings. |
| 13 | +Material maintains usage of the framework `AlertDialog`, but provides a new builder, `MaterialAlertDialogBuilder`, which configures the instantiated `AlertDialog` with Material specs and theming. |
| 14 | +By default, a Material `AlertDialog` darkens the background with a scrim and appears in the center of the viewport. It is a fixed size based upon screen orientation and size. |
| 15 | + |
| 16 | +## Design & API Documentation |
| 17 | + |
| 18 | +- [Material Design guidelines: Dialogs](https://material.io/design/components/dialogs.html) |
| 19 | + <!--{: .icon-list-item.icon-list-item--spec }--> |
| 20 | +- [Class definition](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/dialog/MaterialAlertDialogBuilder.java) |
| 21 | + <!--{: .icon-list-item.icon-list-item--link }--> |
| 22 | +- [Class overview](https://developer.android.com/reference/android/app/AlertDialog) |
| 23 | + <!--{: .icon-list-item.icon-list-item--link }--> <!--{: .icon-list }--> |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +The `MaterialAlertDialogBuilder` allows creation of a Material `AlertDialog`. `MaterialAlertDialogBuilder` extends `AlertDialog.Builder` and passes through all builder methods changing the return type to a 'MaterialAlertDialogBuilder'. |
| 28 | + |
| 29 | +```java |
| 30 | + new MaterialAlertDialogBuilder(context) |
| 31 | + .setTitle("Title") |
| 32 | + .setMessage("Message") |
| 33 | + .setPositiveButton("Ok", null) |
| 34 | + .show(); |
| 35 | +``` |
| 36 | + |
| 37 | +### Material Theming |
| 38 | + |
| 39 | +`MaterialAlertDialogBuilder` requires that your application use a Material Components theme (e.g., `Theme.MaterialComponents.Light`). Using a Material Components theme with `MaterialAlertDialogBuilder` will result in an `AlertDialog` that matches your appplication's color, typography, and shape theming. |
| 40 | + |
| 41 | +### Styles |
| 42 | + |
| 43 | +To change the appearance of every `AlertDialog` built with a `MaterialAlertDialogBuilder`, use the `materialAlertDialogTheme` attribute. |
| 44 | + |
| 45 | +```xml |
| 46 | + <item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog</item> |
| 47 | +``` |
| 48 | + |
| 49 | +To change an individual `AlertDialog`, pass-in a `themeResId` to the constructor of a `MaterialAlertDialogBuilder` instanced. |
| 50 | + |
| 51 | +```java |
| 52 | +new MaterialAlertDialogBuilder(context, R.style.ThemeOverlay_MaterialComponents_MaterialAlertDialog) |
| 53 | +``` |
| 54 | + |
| 55 | +The `materialAlertDialogTheme` attribute supports additional choices: |
| 56 | + |
| 57 | +```xml |
| 58 | +<item name="materialAlertDialogTitlePanelStyle">@style/MaterialAlertDialog.MaterialComponents.Title.Panel</item> |
| 59 | +<item name="materialAlertDialogTitleIconStyle">@style/MaterialAlertDialog.MaterialComponents.Title.Icon</item> |
| 60 | +<item name="materialAlertDialogTitleTextStyle">@style/MaterialAlertDialog.MaterialComponents.Title.Text</item>item> |
| 61 | +``` |
| 62 | + |
| 63 | +`AlertDialog` objects created by a `MaterialAlertDialogBuilder` will also respond to these additional attributes set in `alertDialogStyle` that help position the window. |
| 64 | + |
| 65 | +```xml |
| 66 | +<attr name="backgroundInsetStart" format="dimension"/> |
| 67 | +<attr name="backgroundInsetTop" format="dimension"/> |
| 68 | +<attr name="backgroundInsetEnd" format="dimension"/> |
| 69 | +<attr name="backgroundInsetBottom" format="dimension"/> |
| 70 | +``` |
| 71 | + |
| 72 | +### Template Styles |
| 73 | + |
| 74 | +For the common case of a centered title, use `ThemeOverlay.MaterialComponents.MaterialAlertDialog.Centered`. |
| 75 | + |
| 76 | +```java |
| 77 | +new MaterialAlertDialogBuilder(context, R.style.ThemeOverlay_MaterialComponents_MaterialAlertDialog_Centered) |
| 78 | + .setTitle("Title") |
| 79 | + .setMessage("Message") |
| 80 | + .setPositiveButton("Accept", /* listener = */ null)); |
| 81 | +``` |
2 | 82 |
|
3 |
| -The [Dialog component](https://material.io/go/design-dialogs) is yet to be |
4 |
| -completed, please follow the [tracking |
5 |
| -issue](https://github.com/material-components/material-components-android/issues/77) |
6 |
| -for more information. |
|
0 commit comments