Skip to content

Commit

Permalink
[Dialog] Made dialog background color configurable in XML styles.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 522443049
  • Loading branch information
pekingme authored and paulfthomas committed Apr 10, 2023
1 parent 6b28ffc commit 303fabd
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/components/Dialog.md
Expand Up @@ -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`<br/>`app:shapeAppearanceOverlay` | N/A | `?attr/shapeAppearanceMediumComponent` with a corner size of `28dp`
**Background inset start and end** | `app:backgroundInsetStart`<br/>`app:backgroundInsetEnd` | `setBackgroundInsetStart`<br/>`setBackgroundInsetEnd` | `24dp`
**Background inset top and bottom** | `app:backgroundInsetTop`<br/>`app:backgroundInsetBottom` | `setBackgroundInsetTop`<br/>`setBackgroundInsetBottom` | `80dp`
Expand Down
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -50,7 +50,6 @@
import androidx.annotation.StyleRes;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.core.view.ViewCompat;
import com.google.android.material.color.MaterialColors;
import com.google.android.material.resources.MaterialAttributes;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
Expand All @@ -74,7 +73,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 =
Expand Down Expand Up @@ -117,12 +116,16 @@ public MaterialAlertDialogBuilder(@NonNull Context context, int overrideThemeRes
backgroundInsets =
MaterialDialogs.getDialogBackgroundInsets(context, DEF_STYLE_ATTR, DEF_STYLE_RES);

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, 0);
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) {
Expand Down
Expand Up @@ -29,6 +29,7 @@
</declare-styleable>

<declare-styleable name="MaterialAlertDialog">
<attr name="backgroundTint"/>
<attr name="backgroundInsetStart" format="dimension"/>
<attr name="backgroundInsetTop" format="dimension"/>
<attr name="backgroundInsetEnd" format="dimension"/>
Expand Down
Expand Up @@ -27,6 +27,7 @@
</style>

<style name="MaterialAlertDialog.MaterialComponents" parent="AlertDialog.AppCompat">
<item name="backgroundTint">?attr/colorSurface</item>
<item name="android:layout">@layout/mtrl_alert_dialog</item>
<item name="listItemLayout">@layout/mtrl_alert_select_dialog_item</item>
<item name="multiChoiceItemLayout">@layout/mtrl_alert_select_dialog_multichoice</item>
Expand Down Expand Up @@ -111,6 +112,7 @@
</style>

<style name="MaterialAlertDialog.Material3" parent="MaterialAlertDialog.MaterialComponents">
<item name="backgroundTint">@macro/m3_comp_dialog_container_color</item>
<item name="enforceMaterialTheme">true</item>
<item name="android:layout">@layout/m3_alert_dialog</item>
<item name="shapeAppearance">@macro/m3_comp_dialog_container_shape</item>
Expand Down
Expand Up @@ -21,6 +21,7 @@

<!-- Generated from token set (md.comp.dialog) in context (platform=android, audience=3p). -->
<!-- Enabled - Container -->
<macro name="m3_comp_dialog_container_color">?attr/colorSurface</macro>
<macro name="m3_comp_dialog_container_shape">?attr/shapeAppearanceCornerExtraLarge</macro>
<!-- Enabled - Headline -->
<macro name="m3_comp_dialog_headline_type">?attr/textAppearanceHeadlineSmall</macro>
Expand Down

0 comments on commit 303fabd

Please sign in to comment.