Skip to content

Commit d7a07ab

Browse files
Material Design Teamhunterstich
authored andcommitted
[M3][Color] Enable Resources Harmonization with Material defaults in Catalog app.
PiperOrigin-RevId: 430775729
1 parent 869d943 commit d7a07ab

File tree

3 files changed

+58
-28
lines changed

3 files changed

+58
-28
lines changed

catalog/java/io/material/catalog/color/res/layout/cat_colors_fragment.xml

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,53 +24,68 @@
2424
<LinearLayout
2525
android:layout_width="match_parent"
2626
android:layout_height="wrap_content"
27-
android:orientation="horizontal"
28-
android:baselineAligned="false">
27+
android:orientation="vertical">
2928

3029
<LinearLayout
31-
android:layout_width="0dp"
30+
android:layout_width="match_parent"
3231
android:layout_height="wrap_content"
33-
android:orientation="vertical"
34-
android:padding="8dp"
35-
android:layout_weight="1">
32+
android:orientation="horizontal"
33+
android:baselineAligned="false">
3634

3735
<LinearLayout
38-
android:id="@+id/cat_colors_surfaces"
39-
android:layout_width="match_parent"
36+
android:layout_width="0dp"
4037
android:layout_height="wrap_content"
41-
android:orientation="vertical">
38+
android:orientation="vertical"
39+
android:padding="8dp"
40+
android:layout_weight="1">
4241

43-
<TextView
42+
<LinearLayout
43+
android:id="@+id/cat_colors_surfaces"
44+
android:layout_width="match_parent"
45+
android:layout_height="wrap_content"
46+
android:orientation="vertical">
47+
48+
<TextView
49+
android:layout_width="match_parent"
50+
android:layout_height="wrap_content"
51+
android:paddingBottom="8dp"
52+
android:text="@string/cat_color_surfaces" />
53+
</LinearLayout>
54+
55+
<LinearLayout
56+
android:id="@+id/cat_colors_utility"
4457
android:layout_width="match_parent"
4558
android:layout_height="wrap_content"
46-
android:text="@string/cat_color_surfaces" />
59+
android:orientation="vertical">
60+
61+
<TextView
62+
android:layout_width="match_parent"
63+
android:layout_height="wrap_content"
64+
android:paddingBottom="8dp"
65+
android:text="@string/cat_color_utility" />
66+
</LinearLayout>
4767
</LinearLayout>
4868

4969
<LinearLayout
50-
android:id="@+id/cat_colors_utility"
51-
android:layout_width="match_parent"
70+
android:id="@+id/cat_colors_content"
71+
android:layout_width="0dp"
5272
android:layout_height="wrap_content"
53-
android:orientation="vertical">
73+
android:orientation="vertical"
74+
android:padding="8dp"
75+
android:layout_weight="1">
5476

5577
<TextView
5678
android:layout_width="match_parent"
5779
android:layout_height="wrap_content"
58-
android:text="@string/cat_color_utility" />
80+
android:paddingBottom="8dp"
81+
android:text="@string/cat_color_content" />
5982
</LinearLayout>
6083
</LinearLayout>
6184

62-
<LinearLayout
63-
android:id="@+id/cat_colors_content"
64-
android:layout_width="0dp"
85+
<TextView
86+
android:layout_width="match_parent"
6587
android:layout_height="wrap_content"
66-
android:orientation="vertical"
6788
android:padding="8dp"
68-
android:layout_weight="1">
69-
70-
<TextView
71-
android:layout_width="match_parent"
72-
android:layout_height="wrap_content"
73-
android:text="@string/cat_color_content" />
74-
</LinearLayout>
89+
android:text="@string/cat_color_utility_note" />
7590
</LinearLayout>
7691
</ScrollView>

catalog/java/io/material/catalog/color/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<string name="cat_color_surfaces" translatable="false">Surfaces</string>
4848
<string name="cat_color_utility" translatable="false">Utility</string>
4949
<string name="cat_color_content" translatable="false">Content</string>
50+
<string name="cat_color_utility_note" translatable="false">Note: When dynamic color is enabled, the Error color roles will look different from baseline because they will be harmonized with Primary by default.</string>
5051

5152
<string name="cat_color_role_primary" translatable="false">Primary</string>
5253
<string name="cat_color_role_on_primary" translatable="false">On Primary</string>

catalog/java/io/material/catalog/preferences/DynamicColorPreference.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import androidx.annotation.NonNull;
2424
import com.google.android.material.color.DynamicColors;
2525
import com.google.android.material.color.DynamicColors.Precondition;
26+
import com.google.android.material.color.DynamicColorsOptions;
27+
import com.google.android.material.color.HarmonizedColors;
28+
import com.google.android.material.color.HarmonizedColorsOptions;
2629
import com.google.common.collect.ImmutableList;
2730

2831
/** Dynamic color preference to enable/disable dynamic colors. */
@@ -75,13 +78,24 @@ protected void apply(@NonNull Context context, @NonNull Option selectedOption) {
7578
isOptionOn = selectedOption.id == OPTION_ID_ON;
7679
if (isOptionOn && !isApplied) {
7780
isApplied = true;
78-
DynamicColors.applyToActivitiesIfAvailable(
79-
(Application) context.getApplicationContext(), precondition);
81+
// TODO(b/221246424): Add preference option to turn on/off the color harmonization.
82+
applyDynamicColorsWithMaterialDefaultHarmonization(context);
8083
}
8184
}
8285

8386
@Override
8487
protected boolean shouldRecreateActivityOnOptionChanged() {
8588
return true;
8689
}
90+
91+
private void applyDynamicColorsWithMaterialDefaultHarmonization(@NonNull Context context) {
92+
DynamicColors.applyIfAvailable(
93+
new DynamicColorsOptions.Builder((Application) context.getApplicationContext())
94+
.setPrecondition(precondition)
95+
.setOnAppliedCallback(
96+
activity ->
97+
HarmonizedColors.applyIfAvailable(
98+
HarmonizedColorsOptions.createMaterialDefaults(activity)))
99+
.build());
100+
}
87101
}

0 commit comments

Comments
 (0)