From 77cdc1e3ace0ddc22e8cb2b4cc40c176835f6149 Mon Sep 17 00:00:00 2001 From: pekingme <8545955+pekingme@users.noreply.github.com> Date: Tue, 11 Apr 2023 10:16:48 -0700 Subject: [PATCH] [Dialog] Made dialog background color configurable in XML styles. PiperOrigin-RevId: 523436908 --- docs/components/Dialog.md | 2 +- .../material/dialog/MaterialAlertDialogBuilder.java | 13 ++++++++++--- .../android/material/dialog/res/values/attrs.xml | 1 + .../android/material/dialog/res/values/styles.xml | 2 ++ .../android/material/dialog/res/values/tokens.xml | 1 + 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docs/components/Dialog.md b/docs/components/Dialog.md index 96566da6b1d..c9bdf7407e8 100644 --- a/docs/components/Dialog.md +++ b/docs/components/Dialog.md @@ -120,7 +120,7 @@ particular type), a background scrim, and, optionally, title and buttons. Element | **Attribute** | **Related methods** | **Default value** ----------------------------------- | -------------------------------------------------------- | ------------------------------------------------------ | ----------------- -**Color** | N/A | N/A | `?attr/colorSurface` +**Color** | `app:backgroundTint` | N/A | `?attr/colorSurface` **Shape** | `app:shapeAppearance`
`app:shapeAppearanceOverlay` | N/A | `?attr/shapeAppearanceMediumComponent` with a corner size of `28dp` **Background inset start and end** | `app:backgroundInsetStart`
`app:backgroundInsetEnd` | `setBackgroundInsetStart`
`setBackgroundInsetEnd` | `24dp` **Background inset top and bottom** | `app:backgroundInsetTop`
`app:backgroundInsetBottom` | `setBackgroundInsetTop`
`setBackgroundInsetBottom` | `80dp` diff --git a/lib/java/com/google/android/material/dialog/MaterialAlertDialogBuilder.java b/lib/java/com/google/android/material/dialog/MaterialAlertDialogBuilder.java index 4064e50bb21..ae8647e2ae5 100644 --- a/lib/java/com/google/android/material/dialog/MaterialAlertDialogBuilder.java +++ b/lib/java/com/google/android/material/dialog/MaterialAlertDialogBuilder.java @@ -28,6 +28,7 @@ import android.content.DialogInterface.OnMultiChoiceClickListener; import android.content.res.ColorStateList; import android.content.res.Resources.Theme; +import android.content.res.TypedArray; import android.database.Cursor; import android.graphics.Rect; import android.graphics.drawable.Drawable; @@ -41,7 +42,6 @@ import android.widget.ListAdapter; import androidx.annotation.ArrayRes; import androidx.annotation.AttrRes; -import androidx.annotation.Dimension; import androidx.annotation.DrawableRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -74,7 +74,7 @@ public class MaterialAlertDialogBuilder extends AlertDialog.Builder { private static final int MATERIAL_ALERT_DIALOG_THEME_OVERLAY = R.attr.materialAlertDialogTheme; @Nullable private Drawable background; - @NonNull @Dimension private final Rect backgroundInsets; + @NonNull private final Rect backgroundInsets; private static int getMaterialAlertDialogThemeOverlay(@NonNull Context context) { TypedValue materialAlertDialogThemeOverlay = @@ -119,10 +119,17 @@ public MaterialAlertDialogBuilder(@NonNull Context context, int overrideThemeRes int surfaceColor = MaterialColors.getColor(context, R.attr.colorSurface, getClass().getCanonicalName()); + + TypedArray a = + context.obtainStyledAttributes( + /* set= */ null, R.styleable.MaterialAlertDialog, DEF_STYLE_ATTR, DEF_STYLE_RES); + int backgroundColor = a.getColor(R.styleable.MaterialAlertDialog_backgroundTint, surfaceColor); + a.recycle(); + MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable(context, null, DEF_STYLE_ATTR, DEF_STYLE_RES); materialShapeDrawable.initializeElevationOverlay(context); - materialShapeDrawable.setFillColor(ColorStateList.valueOf(surfaceColor)); + materialShapeDrawable.setFillColor(ColorStateList.valueOf(backgroundColor)); // dialogCornerRadius first appeared in Android Pie if (Build.VERSION.SDK_INT >= VERSION_CODES.P) { diff --git a/lib/java/com/google/android/material/dialog/res/values/attrs.xml b/lib/java/com/google/android/material/dialog/res/values/attrs.xml index 6f0f6c22819..78ac3e17b91 100644 --- a/lib/java/com/google/android/material/dialog/res/values/attrs.xml +++ b/lib/java/com/google/android/material/dialog/res/values/attrs.xml @@ -29,6 +29,7 @@ + diff --git a/lib/java/com/google/android/material/dialog/res/values/styles.xml b/lib/java/com/google/android/material/dialog/res/values/styles.xml index f188ed1859c..1ef513f4564 100644 --- a/lib/java/com/google/android/material/dialog/res/values/styles.xml +++ b/lib/java/com/google/android/material/dialog/res/values/styles.xml @@ -27,6 +27,7 @@