xref: /aosp_15_r20/frameworks/base/packages/SystemUI/docs/dialogs.md (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1*d57664e9SAndroid Build Coastguard Worker# Dialogs in SystemUI
2*d57664e9SAndroid Build Coastguard Worker
3*d57664e9SAndroid Build Coastguard Worker## Creating a dialog
4*d57664e9SAndroid Build Coastguard Worker
5*d57664e9SAndroid Build Coastguard Worker### Styling
6*d57664e9SAndroid Build Coastguard Worker
7*d57664e9SAndroid Build Coastguard WorkerIn order to have uniform styling in dialogs, use [SystemUIDialog][1] with its default theme.
8*d57664e9SAndroid Build Coastguard WorkerIf not possible, use [AlertDialog][2] with the SystemUI theme `R.style.Theme_SystemUI_Dialog`.
9*d57664e9SAndroid Build Coastguard WorkerIf needed, consider extending this theme instead of creating a new one.
10*d57664e9SAndroid Build Coastguard Worker
11*d57664e9SAndroid Build Coastguard Worker### Setting the internal elements
12*d57664e9SAndroid Build Coastguard Worker
13*d57664e9SAndroid Build Coastguard WorkerThe internal elements of the dialog are laid out using the following resources:
14*d57664e9SAndroid Build Coastguard Worker
15*d57664e9SAndroid Build Coastguard Worker* [@layout/alert_dialog_systemui][3]
16*d57664e9SAndroid Build Coastguard Worker* [@layout/alert_dialog_title_systemui][4]
17*d57664e9SAndroid Build Coastguard Worker* [@layout/alert_dialog_button_bar_systemui][5]
18*d57664e9SAndroid Build Coastguard Worker
19*d57664e9SAndroid Build Coastguard WorkerUse the default components of the layout by calling the appropriate setters (in the dialog or
20*d57664e9SAndroid Build Coastguard Worker[AlertDialog.Builder][2]). The supported styled setters are:
21*d57664e9SAndroid Build Coastguard Worker
22*d57664e9SAndroid Build Coastguard Worker* `setIcon`: tinted using `attr/colorAccentPrimaryVariant`.
23*d57664e9SAndroid Build Coastguard Worker* `setTitle`: this will use `R.style.TextAppearance_Dialog_Title`.
24*d57664e9SAndroid Build Coastguard Worker* `setMessage`: this will use `R.style.TextAppearance_Dialog_Body_Message`.
25*d57664e9SAndroid Build Coastguard Worker* `SystemUIDialog.set<Positive|Negative|Neutral>Button` or `AlertDialog.setButton`: this will use
26*d57664e9SAndroid Build Coastguard Worker   the following styles for buttons.
27*d57664e9SAndroid Build Coastguard Worker  * `R.style.Widget_Dialog_Button` for the positive button.
28*d57664e9SAndroid Build Coastguard Worker  * `R.style.Widget_Dialog_Button_BorderButton` for the negative and neutral buttons.
29*d57664e9SAndroid Build Coastguard Worker
30*d57664e9SAndroid Build Coastguard Worker  If needed to use the same style for all three buttons, the style attributes
31*d57664e9SAndroid Build Coastguard Worker  `?android:attr/buttonBar<Positive|NegativeNeutral>Button` can be overriden in a theme that extends
32*d57664e9SAndroid Build Coastguard Worker  from `R.style.Theme_SystemUI_Dialog`.
33*d57664e9SAndroid Build Coastguard Worker* `setView`: to set a custom view in the dialog instead of using `setMessage`.
34*d57664e9SAndroid Build Coastguard Worker
35*d57664e9SAndroid Build Coastguard WorkerUsing `setContentView` is discouraged as this replaces the content completely.
36*d57664e9SAndroid Build Coastguard Worker
37*d57664e9SAndroid Build Coastguard WorkerAll these calls should be made before `Dialog#create` or `Dialog#show` (which internally calls
38*d57664e9SAndroid Build Coastguard Worker`create`) are called, as that's when the content is installed.
39*d57664e9SAndroid Build Coastguard Worker
40*d57664e9SAndroid Build Coastguard Worker## Showing the dialog
41*d57664e9SAndroid Build Coastguard Worker
42*d57664e9SAndroid Build Coastguard WorkerWhen showing a dialog triggered by clicking on a `View`, you should use [DialogTransitionAnimator][6] to
43*d57664e9SAndroid Build Coastguard Workernicely animate the dialog from/to that `View`, instead of calling `Dialog.show`.
44*d57664e9SAndroid Build Coastguard Worker
45*d57664e9SAndroid Build Coastguard WorkerThis animator provides the following methods:
46*d57664e9SAndroid Build Coastguard Worker
47*d57664e9SAndroid Build Coastguard Worker* `showFromView`: animates the dialog show from a view , and the dialog dismissal/cancel/hide to the
48*d57664e9SAndroid Build Coastguard Worker  same view.
49*d57664e9SAndroid Build Coastguard Worker* `showFromDialog`: animates the dialog show from a currently showing dialog, and the dialog
50*d57664e9SAndroid Build Coastguard Worker  dismissal/cancel/hide back to that dialog. The originating dialog must have been shown using
51*d57664e9SAndroid Build Coastguard Worker  `DialogTransitionAnimator`.
52*d57664e9SAndroid Build Coastguard Worker* `dismissStack`: dismisses a stack of dialogs that were launched using `showFromDialog` animating
53*d57664e9SAndroid Build Coastguard Worker  the top-most dialog back into the view that was used in the initial `showFromView`.
54*d57664e9SAndroid Build Coastguard Worker
55*d57664e9SAndroid Build Coastguard Worker## Example
56*d57664e9SAndroid Build Coastguard Worker
57*d57664e9SAndroid Build Coastguard WorkerHere's a short code snippet with an example on how to use the guidelines.
58*d57664e9SAndroid Build Coastguard Worker
59*d57664e9SAndroid Build Coastguard Worker```kotlin
60*d57664e9SAndroid Build Coastguard Workerval dialog = SystemUIDialog(context).apply {
61*d57664e9SAndroid Build Coastguard Worker    setIcon(R.drawable.my_icon)
62*d57664e9SAndroid Build Coastguard Worker    setTitle(context.getString(R.string.title))
63*d57664e9SAndroid Build Coastguard Worker    setMessage(context.getString(R.string.message))
64*d57664e9SAndroid Build Coastguard Worker    // Alternatively to setMessage:
65*d57664e9SAndroid Build Coastguard Worker    // val content = LayoutManager.from(context).inflate(R.layout.content, null)
66*d57664e9SAndroid Build Coastguard Worker    // setView(content)
67*d57664e9SAndroid Build Coastguard Worker    setPositiveButton(R.string.positive_button_text, ::onPositiveButton)
68*d57664e9SAndroid Build Coastguard Worker    setNegativeButton(R.string.negative_button_text, ::onNegativeButton)
69*d57664e9SAndroid Build Coastguard Worker    setNeutralButton(R.string.neutral_button_text, ::onNeutralButton)
70*d57664e9SAndroid Build Coastguard Worker}
71*d57664e9SAndroid Build Coastguard WorkerdialogTransitionAnimator.showFromView(dialog, view)
72*d57664e9SAndroid Build Coastguard Worker```
73*d57664e9SAndroid Build Coastguard Worker
74*d57664e9SAndroid Build Coastguard Worker[1]: /packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java
75*d57664e9SAndroid Build Coastguard Worker[2]: /core/java/android/app/AlertDialog.java
76*d57664e9SAndroid Build Coastguard Worker[3]: /packages/SystemUI/res/layout/alert_dialog_systemui.xml
77*d57664e9SAndroid Build Coastguard Worker[4]: /packages/SystemUI/res/layout/alert_dialog_title_systemui.xml
78*d57664e9SAndroid Build Coastguard Worker[5]: /packages/SystemUI/res/layout/alert_dialog_button_bar_systemui.xml
79*d57664e9SAndroid Build Coastguard Worker[6]: /packages/SystemUI/animation/src/com/android/systemui/animation/DialogTransitionAnimator.kt