# Alert Dialog Box (AlertDialog) You can set the text content and response callback for an alert dialog box. > **NOTE** > > The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. > > The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where the UI context is unclear. For details, see [UIContext](../js-apis-arkui-UIContext.md#uicontext). > > Since API version 10, you can use the [showAlertDialog](../js-apis-arkui-UIContext.md#showalertdialog) API in [UIContext](../js-apis-arkui-UIContext.md#uicontext) to obtain the UI context. ## AlertDialog.show static show(value: AlertDialogParamWithConfirm | AlertDialogParamWithButtons | AlertDialogParamWithOptions) Shows an alert dialog box. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Parameters** | Name | Type | Mandatory| Description| | ---- | --------------- | -------- | -------- | | value | [AlertDialogParamWithConfirm](#alertdialogparamwithconfirm) \| [AlertDialogParamWithButtons](#alertdialogparamwithbuttons) \| [AlertDialogParamWithOptions](#alertdialogparamwithoptions10)10+ | Yes| Defines and displays the **AlertDialog** component.| ## AlertDialogParam | Name | Type | Mandatory| Description | | --------------------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | | title | [ResourceStr](ts-types.md#resourcestr) | No | Title of the dialog box.
**Atomic service API**: This API can be used in atomic services since API version 11. | | subtitle10+ | [ResourceStr](ts-types.md#resourcestr) | No | Subtitle of the dialog box.
**Atomic service API**: This API can be used in atomic services since API version 11. | | message | [ResourceStr](ts-types.md#resourcestr) | Yes | Content of the dialog box.
**Atomic service API**: This API can be used in atomic services since API version 11. | | autoCancel | boolean | No | Whether to close the dialog box when the overlay is clicked. The value **true** means to close the dialog box when the overlay is clicked, and **false** means the opposite.
Default value: **true**
**Atomic service API**: This API can be used in atomic services since API version 11.| | cancel | () => void | No | Callback invoked when the dialog box is closed after the overlay is clicked.
**Atomic service API**: This API can be used in atomic services since API version 11. | | alignment | [DialogAlignment](#dialogalignment) | No | Alignment mode of the dialog box in the vertical direction.
Default value: **DialogAlignment.Default**
**Atomic service API**: This API can be used in atomic services since API version 11.
**NOTE**
If **showInSubWindow** is set to **true** in **UIExtension**, the dialog box is aligned with the host window based on **UIExtension**.| | offset | [Offset](ts-types.md#offset) | No | Offset of the dialog box relative to the alignment position.
Default value: **{ dx: 0 , dy: 0 }**
**Atomic service API**: This API can be used in atomic services since API version 11.| | gridCount | number | No | Number of grid columns occupied by the width of the dialog box.
Default value: **4**
**Atomic service API**: This API can be used in atomic services since API version 11. | | maskRect10+ | [Rectangle](#rectangle8) | No | Mask area of the dialog box. Events outside the mask area are transparently transmitted, and events within the mask area are not.
Default value: **{ x: 0, y: 0, width: '100%', height: '100%' }**
**NOTE**
**maskRect** does not take effect when **showInSubWindow** is set to **true**.
**Atomic service API**: This API can be used in atomic services since API version 12.| | showInSubWindow11+ | boolean | No | Whether to show the dialog box in a sub-window when the dialog box needs to be displayed outside the main window.
Default value: **false**
**NOTE**
A dialog box whose **showInSubWindow** attribute is **true** cannot trigger the display of another dialog box whose **showInSubWindow** attribute is also **true**.
**Atomic service API**: This API can be used in atomic services since API version 12.| | isModal11+ | boolean | No | Whether the dialog box is a modal. A modal dialog box has a mask applied, while a non-modal dialog box does not.
Default value: **true**
**Atomic service API**: This API can be used in atomic services since API version 12.| | backgroundColor11+ | [ResourceColor](ts-types.md#resourcecolor) | No | Background color of the dialog box.
Default value: **Color.Transparent**
**Atomic service API**: This API can be used in atomic services since API version 12. | | backgroundBlurStyle11+ | [BlurStyle](ts-appendix-enums.md#blurstyle9) | No | Background blur style of the dialog box.
Default value: **BlurStyle.COMPONENT_ULTRA_THICK**
**Atomic service API**: This API can be used in atomic services since API version 12.| | onWillDismiss12+ | Callback<[DismissDialogAction](#dismissdialogaction12)> | No | Callback for interactive closure of the dialog box.
**NOTE**
1. If this callback is registered, the dialog box will not be closed immediately after the user touches the mask or the Back button, presses the Esc key, or swipes left or right on the screen. The **reason** parameter in the callback is used to determine whether the dialog box can be closed. The reason returned by the component does not support the value **CLOSE_BUTTON**.
2. In the **onWillDismiss** callback, another **onWillDismiss** callback is not allowed.| | cornerRadius12+ | [BorderRadiuses](ts-types.md#borderradiuses9) \| [Dimension](ts-types.md#dimension10) | No | Radius of the rounded corners of the background.
You can set separate radiuses for the four rounded corners.
Default value: **{ topLeft: '32vp', topRight: '32vp', bottomLeft: '32vp', bottomRight: '32vp' }**
The radius of the rounded corners is subject to the component size. Its maximum value is half of the component width or height. If the value is negative, the default value is used.
When set to a percentage, the value defines the radius as a percentage of the parent component's width or height.| | transition12+ | [TransitionEffect](ts-transition-animation-component.md#transitioneffect10) | No | Transition effect for the entrance and exit of the dialog box.
**NOTE**
1. If this parameter is not set, the default effect is used.
2. Touching the Back button during the entrance animation pauses the entrance animation and starts the exit animation. The final effect is one obtained after the curves of the entrance and exit animations are combined.
3. Touching the Back button during the exit animation does not affect the animation playback. Touching the Back button again closes the application. | | width12+ | [Dimension](ts-types.md#dimension10) | No | Width of the dialog box.
**NOTE**
- Default maximum width of the dialog box: 400 vp
- When this parameter is set to a percentage, the reference width of the dialog box is the width of the window where the dialog box is located. You can decrease or increase the width as needed.| | height12+ | [Dimension](ts-types.md#dimension10) | No | Height of the dialog box.
**NOTE**
- Default maximum height of the dialog box: 0.9 x (Window height – Safe area)
- When this parameter is set to a percentage, the reference height of the dialog box is the height of the window where the dialog box is located minus the safe area. You can decrease or increase the height as needed.| | borderWidth12+ | [Dimension](ts-types.md#dimension10) \| [EdgeWidths](ts-types.md#edgewidths9) | No |
You can set the width for all four sides or set separate widths for individual sides.
Default value: **0**
When set to a percentage, the value defines the border width as a percentage of the parent dialog box's width.
If the left and right borders are greater than its width, or the top and bottom borders are greater than its height, the dialog box may not display as expected.| | borderColor12+ | [ResourceColor](ts-types.md#resourcecolor) \| [EdgeColors](ts-types.md#edgecolors9) | No | Border color of the dialog box.
Default value: **Color.Black**
**borderColor** must be used with **borderWidth** in pairs.| | borderStyle12+ | [BorderStyle](ts-appendix-enums.md#borderstyle) \| [EdgeStyles](ts-types.md#edgestyles9) | No | Border style of the dialog box.
Default value: **BorderStyle**
**borderStyle** must be used with **borderWidth** in pairs.| | shadow12+ | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions) \| [ShadowStyle](ts-universal-attributes-image-effect.md#shadowstyle10) | No | Shadow of the dialog box.
Default value on 2-in-1 devices: **ShadowStyle.OUTER_FLOATING_MD** when the dialog box is focused and **ShadowStyle.OUTER_FLOATING_SM** otherwise| ## AlertDialogParamWithConfirm Inherits from [AlertDialogParam](#alertdialogparam). **Atomic service API**: This API can be used in atomic services since API version 11. | Name | Type | Mandatory | Description | | ---------- | ---------------- | ---------- | ------------------------------- | | confirm | {
enabled10+?: boolean,
defaultFocus10+?: boolean,
style10+?: [DialogButtonStyle](#dialogbuttonstyle10),
value: [ResourceStr](ts-types.md#resourcestr),
fontColor?: [ResourceColor](ts-types.md#resourcecolor),
backgroundColor?: [ResourceColor](ts-types.md#resourcecolor),
action: () => void
} | No | Information about the confirm button. When the dialog box has focus and the **Tab** key is not pressed for sequential focus navigation, the button responds to the **Enter** key by default.
**enabled**: whether to respond when the button is clicked. The value **true** means to respond when the button is clicked, and **false** means the opposite.
Default value: **true**
**defaultFocus**: whether the button is the default focus. The value **true** means that the button is the default focus, and **false** means the opposite.
Default value: **false**
**style**: button style.
Default value: **DialogButtonStyle.DEFAULT**
**value**: button text.
**fontColor**: font color of the button.
**backgroundColor**: background color of the button.
**action**: callback upon button clicking.| Priorities of the **confirm** parameters: **fontColor** and **backgroundColor** > **style** > **defaultFocus** | backgroundColor | fontColor | style | defaultFocus | Effect | | --------------- | --------- | --------------------------- | ------------ | -------- | | Green | Red | - | - | Red text on green background| | Green | - | DialogButtonStyle.HIGHLIGHT | - | White text on green background| | Green | - | DialogButtonStyle.DEFAULT | - | Blue text on green background| | Green | - | - | TRUE | White text on green background| | Green | - | - | FALSE/- | Blue text on green background| | - | Red | DialogButtonStyle.HIGHLIGHT | - | Red text on blue background| | - | Red | DialogButtonStyle.DEFAULT | - | Red text on white background| | - | Red | - | TRUE | Red text on blue background| | - | Red | - | FALSE/- | Red text on white background| | - | - | DialogButtonStyle.HIGHLIGHT | - | White text on blue background| | - | - | DialogButtonStyle.DEFAULT | - | Blue text on white background| | - | - | - | TRUE | White text on blue background| | - | - | - | FALSE/- | Blue text on white background| ## AlertDialogParamWithButtons Inherits from [AlertDialogParam](#alertdialogparam). **Atomic service API**: This API can be used in atomic services since API version 11. | Name | Type | Mandatory | Description | | --------------- | ---------------------- | ------------ | --------------------- | | primaryButton | {
enabled10+?: boolean,
defaultFocus10+?: boolean,
style10+?: [DialogButtonStyle](#dialogbuttonstyle10),
value: [ResourceStr](ts-types.md#resourcestr),
fontColor?: [ResourceColor](ts-types.md#resourcecolor),
backgroundColor?: [ResourceColor](ts-types.md#resourcecolor),
action: () => void;
} | No| Information about the confirm button. When the dialog box has focus and the **Tab** key is not pressed for sequential focus navigation, the button responds to the **Enter** key by default.
**enabled**: whether to respond when the button is clicked.
Default value: **true**
**defaultFocus**: whether the button is the default focus.
Default value: **false**
**style**: button style.
Default value: **DialogButtonStyle.DEFAULT**
**value**: button text.
**fontColor**: font color of the button.
**backgroundColor**: background color of the button.
**action**: callback upon button clicking.| | secondaryButton | {
enabled10+?: boolean,
defaultFocus10+?: boolean,
style10+?: [DialogButtonStyle](#dialogbuttonstyle10),
value: [ResourceStr](ts-types.md#resourcestr),
fontColor?: [ResourceColor](ts-types.md#resourcecolor),
backgroundColor?: [ResourceColor](ts-types.md#resourcecolor),
action: () => void;
} | No | Information about the confirm button.
**enabled**: whether to respond when the button is clicked.
Default value: **true**
**defaultFocus**: whether the button is the default focus.
Default value: **false**
**style**: button style.
Default value: **DialogButtonStyle.DEFAULT**
**value**: button text.
**fontColor**: font color of the button.
**backgroundColor**: background color of the button.
**action**: callback upon button clicking.| ## AlertDialogParamWithOptions10+ Inherits from [AlertDialogParam](#alertdialogparam). **Atomic service API**: This API can be used in atomic services since API version 11. | Name | Type | Mandatory | Description | | --------------- | ---------------------- | ------------ | --------------------- | | buttons10+ | Array<[AlertDialogButtonOptions](#alertdialogbuttonoptions10)> | Yes | Buttons in the dialog box.| |buttonDirection10+ | [DialogButtonDirection](#dialogbuttondirection10)| No | Direction in which buttons are laid out.
Default value: **DialogButtonDirection.AUTO**
When there are more than three buttons, the Auto mode (which automatically switches to the vertical layout when there are more than two buttons) is recommended. In non-Auto mode, buttons that extend beyond the display area are clipped.| ## AlertDialogButtonOptions10+ **Atomic service API**: This API can be used in atomic services since API version 11. | Name | Type | Mandatory | Description | | ------------------| ---------------------- | ------------ | --------------------- | | enabled | boolean | No | Whether to respond when the button is clicked.
Default value: **true** | | defaultFocus | boolean | No | Whether the button is the default focus.
Default value: **false** | | style | [DialogButtonStyle](#dialogbuttonstyle10) | No | Style of the button.
Default value: **DialogButtonStyle.DEFAULT** | | value | [ResourceStr](ts-types.md#resourcestr) | Yes | Text of the button. If the value is null, the button is not displayed. | | fontColor | [ResourceColor](ts-types.md#resourcecolor) | No | Font color of the button. | | backgroundColor | [ResourceColor](ts-types.md#resourcecolor) | No | Background color of the button. | | action | () => void | Yes | Callback upon button clicking. | | primary12+ | boolean | No | Whether the button responds to the **Enter** key by default when the dialog box has focus and the **Tab** key is not pressed for sequential focus navigation. If there are multiple buttons, set this parameter to **true** for only one button. Otherwise, no button will respond.| ## DialogButtonDirection10+ **Atomic service API**: This API can be used in atomic services since API version 11. | Name | Description | | -------------------------- | --------- | | AUTO | Buttons are laid out horizontally when there are two or fewer buttons and vertically otherwise.| | HORIZONTAL | Buttons are laid out horizontally.| | VERTICAL | Buttons are laid out vertically.| ## DialogAlignment **Atomic service API**: This API can be used in atomic services since API version 11. | Name | Description | | ------------------------ | ------- | | Top | Vertical top alignment.| | Center | Vertical center alignment.| | Bottom | Vertical bottom alignment.| | Default | Default alignment. | | TopStart8+ | Top left alignment. | | TopEnd8+ | Top right alignment. | | CenterStart8+ | Center left alignment. | | CenterEnd8+ | Center right alignment. | | BottomStart8+ | Bottom left alignment. | | BottomEnd8+ | Bottom right alignment. | ## Rectangle8+ The **Rectangle** type is used to represent a mask area of a dialog box. **Atomic service API**: This API can be used in atomic services since API version 11. | Name | Type | Mandatory| Description | |--------|------------------------------|----|-----------------------------------| | x | [Length](ts-types.md#length) | No | X coordinate of the mask area of the dialog box relative to the upper left corner of the window.
Default value: **0vp**| | y | [Length](ts-types.md#length) | No | Y coordinate of the mask area of the dialog box relative to the upper left corner of the window.
Default value: **0vp**| | width | [Length](ts-types.md#length) | No | Width of the mask area of the dialog box.
Default value: **'100%'** | | height | [Length](ts-types.md#length) | No | Height of the mask area of the dialog box.
Default value: **'100%'** | > **NOTE** > > **x** and **y** can be set to a positive or negative percentage value. When **x** is set to **'100%'**, the mask area is moved rightward by the window width. When **x** is set to **'-100%'**, the mask area is moved leftward by the window width. When **y** is set to **'100%'**, the mask area is moved downward by the window height. When **y** is set to **'-100%'**, the mask area is moved upward by the window height. > > **width** and **height** can be set in percentage and can only be set to positive values. If they are set to negative values, the default values are used instead. > > The percentage is calculated based on the width and height of the window. ## DialogButtonStyle10+ **Atomic service API**: This API can be used in atomic services since API version 11. | Name | Description | | --------- | --------------------------------- | | DEFAULT | Blue text on white background (black background under the dark theme).| | HIGHLIGHT | White text on blue background. | ## DismissDialogAction12+ | Name | Type | Mandatory| Description | |----------|-------------------------------------|------|----------------------------------------------------------------| | dismiss | Callback<void> | Yes | Callback for closing the dialog box. This API is called only when the dialog box needs to be exited. | | reason | [DismissReason](#dismissreason12) | Yes | Reason why the dialog box cannot be closed. You must specify whether to close the dialog box for each of the listed actions.| ## DismissReason12+ | Name | Description | | --------------- | -------------------------------- | | PRESS_BACK | Touching the Back button, swiping left or right on the screen, or pressing the Esc key. | | TOUCH_OUTSIDE | Touching the mask. | | CLOSE_BUTTON | Touching the Close button. | ## Example ### Example 1 ```ts // xxx.ets @Entry @Component struct AlertDialogExample { build() { Column({ space: 5 }) { Button('one button dialog') .onClick(() => { AlertDialog.show( { title: 'title', message: 'text', autoCancel: true, alignment: DialogAlignment.Bottom, offset: { dx: 0, dy: -20 }, gridCount: 3, confirm: { value: 'button', action: () => { console.info('Button-clicking callback') } }, cancel: () => { console.info('Closed callbacks') }, onWillDismiss:(dismissDialogAction: DismissDialogAction)=> { console.info("reason=" + JSON.stringify(dismissDialogAction.reason)) console.log("dialog onWillDismiss") if (dismissDialogAction.reason == DismissReason.PRESS_BACK) { dismissDialogAction.dismiss() } if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) { dismissDialogAction.dismiss() } } } ) }) .backgroundColor(0x317aff) Button('two button dialog') .onClick(() => { AlertDialog.show( { title: 'title', subtitle: 'subtitle', message: 'text', autoCancel: true, alignment: DialogAlignment.Bottom, gridCount: 4, offset: { dx: 0, dy: -20 }, primaryButton: { value: 'cancel', action: () => { console.info('Callback when the first button is clicked') } }, secondaryButton: { enabled: true, defaultFocus: true, style: DialogButtonStyle.HIGHLIGHT, value: 'ok', action: () => { console.info('Callback when the second button is clicked') } }, cancel: () => { console.info('Closed callbacks') }, onWillDismiss:(dismissDialogAction: DismissDialogAction)=> { console.info("reason=" + JSON.stringify(dismissDialogAction.reason)) console.log("dialog onWillDismiss") if (dismissDialogAction.reason == DismissReason.PRESS_BACK) { dismissDialogAction.dismiss() } if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) { dismissDialogAction.dismiss() } } } ) }).backgroundColor(0x317aff) Button('three button dialog') .onClick(() => { AlertDialog.show( { title: 'title', subtitle: 'subtitle', message: 'text', autoCancel: true, alignment: DialogAlignment.Bottom, gridCount: 4, offset: { dx: 0, dy: -20 }, buttonDirection: DialogButtonDirection.HORIZONTAL, buttons: [ { value: 'Button', action: () => { console.info('Callback when button1 is clicked') } }, { value: 'Button', action: () => { console.info('Callback when button2 is clicked') } }, { value: 'Button', enabled: true, defaultFocus: true, style: DialogButtonStyle.HIGHLIGHT, action: () => { console.info('Callback when button3 is clicked') } }, ], cancel: () => { console.info('Closed callbacks') }, onWillDismiss:(dismissDialogAction: DismissDialogAction)=> { console.info("reason=" + JSON.stringify(dismissDialogAction.reason)) console.log("dialog onWillDismiss") if (dismissDialogAction.reason == DismissReason.PRESS_BACK) { dismissDialogAction.dismiss() } if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) { dismissDialogAction.dismiss() } } } ) }).backgroundColor(0x317aff) }.width('100%').margin({ top: 5 }) } } ``` ![en-us_image_alert](figures/en-us_image_alert.gif) ### Example 2 ```ts // xxx.ets @Entry @Component struct AlertDialogExample { build() { Column({ space: 5 }) { Button('one button dialog') .onClick(() => { AlertDialog.show( { title: 'title', subtitle: 'subtitle', message: 'text', autoCancel: true, alignment: DialogAlignment.Center, gridCount: 4, showInSubWindow: true, isModal: true, offset: { dx: 0, dy: -20 }, buttonDirection: DialogButtonDirection.HORIZONTAL, buttons: [ { value: 'Button', action: () => { console.info('Callback when button1 is clicked') } }, { value: 'Button', action: () => { console.info('Callback when button2 is clicked') } }, { value: 'Button', enabled: true, defaultFocus: true, style: DialogButtonStyle.HIGHLIGHT, action: () => { console.info('Callback when button3 is clicked') } }, ], cancel: () => { console.info('Closed callbacks') }, onWillDismiss: (dismissDialogAction: DismissDialogAction) => { console.info("reason=" + JSON.stringify(dismissDialogAction.reason)) console.log("dialog onWillDismiss") if (dismissDialogAction.reason == DismissReason.PRESS_BACK) { dismissDialogAction.dismiss() } if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) { dismissDialogAction.dismiss() } } }) }) }.width('100%').margin({ top: 5 }).backgroundColor(0x317aff) } } ``` ![en-us_image_alert_showinsubwindow](figures/en-us_image_alert_showinsubwindow.jpg) ### Example 3 This example sets a 3s entrance animation and a 100 ms exit animation for the alert dialog. ```ts // xxx.ets import router from '@ohos.router'; @Entry @Component struct AlertDialogExample { build() { Column({ space: 5 }) { Button('AlertDialog Set Duration') .onClick(()=>{ AlertDialog.show( { title: 'AlertDialog 1', message: 'Set Animation Duration open 3 second, close 100ms', autoCancel: true, alignment: DialogAlignment.Top, offset: { dx: 0, dy: -20 }, gridCount: 3, transition:TransitionEffect.asymmetric(TransitionEffect.OPACITY .animation({ duration: 3000, curve: Curve.Sharp }).combine(TransitionEffect.scale({x: 1.5, y: 1.5}).animation({duration: 3000, curve: Curve.Sharp})), TransitionEffect.OPACITY.animation({ duration: 100, curve: Curve.Smooth }) .combine(TransitionEffect.scale({x: 0.5, y: 0.5}).animation({duration: 100, curve: Curve.Smooth}))), confirm: { value: 'button', action: () => { console.info('Button-clicking callback') } }, cancel: () => { console.info('Closed callbacks') } } ) }) .backgroundColor(0x317aff).height("88px") }.width('100%').margin({ top: 5 }) } } ``` ![en-us_image_alert_animation](figures/en-us_image_alert_animation.gif)