@@ -228,24 +228,39 @@ If the app is running on Android S+, dynamic colors will be applied to the
228228activity. You can also apply a custom theme overlay or a precondition as
229229depicted in the application section above.
230230
231- ##### Apply dynamic colors using ` DynamicColorsOptions `
231+ ##### Apply dynamic colors to all activities in the app using ` DynamicColorsOptions `
232232
233- You also have the option to apply dynamic colors by passing in a
234- ` DynamicColorsOptions ` object. When constructing ` DynamicColorsOptions ` , an
235- Application or Activity is required as to where dynamic colors will be applied.
236- You may optionally specify a customized theme overlay, likely inheriting from the
237- ` Material3 ` theme overlays above and/or a precondition, to have finer control over
238- theme overlay deployment. You may also optionally specify a ` OnAppliedCallback `
239- function, which will be called after dynamic colors have been applied:
233+ You also have the option to apply dynamic colors to all activities in the app by
234+ passing in a ` DynamicColorsOptions ` object. When constructing
235+ ` DynamicColorsOptions ` , you may optionally specify a customized theme overlay,
236+ likely inheriting from the ` Material3 ` theme overlays above and/or a
237+ precondition, to have finer control over theme overlay deployment. You may also
238+ optionally specify an ` OnAppliedCallback ` function, which will be called after
239+ dynamic colors have been applied:
240240
241241```
242242DynamicColorsOptions dynamicColorOptions =
243- new DynamicColorsOptions.Builder(application )
243+ new DynamicColorsOptions.Builder()
244244 .setThemeOverlay(themeOverlay)
245245 .setPrecondition(precondition)
246246 .setOnAppliedCallback(onAppliedCallback)
247247 .build()
248- DynamicColors.applyIfAvailable(dynamicColorOptions);
248+ DynamicColors.applyToActivitiesIfAvailable(application, dynamicColorOptions);
249+ ```
250+
251+ ##### Apply dynamic colors to a specific activity using ` DynamicColorsOptions `
252+
253+ You can also apply dynamic colors to a specific activity in the app by passing
254+ in the specific activity and a ` DynamicColorsOptions ` object:
255+
256+ ```
257+ DynamicColorsOptions dynamicColorOptions =
258+ new DynamicColorsOptions.Builder()
259+ .setThemeOverlay(themeOverlay)
260+ .setPrecondition(precondition)
261+ .setOnAppliedCallback(onAppliedCallback)
262+ .build()
263+ DynamicColors.applyToActivityIfAvailable(activity, dynamicColorOptions);
249264```
250265
251266##### Apply dynamic colors to a specific fragment/view
@@ -394,30 +409,41 @@ harmonization won't happen and `colorToHarmonize` will be returned.
394409
395410##### Color Resources Harmonization
396411
397- If you need to harmonize color resources at runtime and use the harmonized color
398- resources in xml, call:
399-
400- ```
401- HarmonizedColors.applyIfAvailable(harmonizedColorsOptions);
402- ```
403- To construct a ` HarmonizedColorsOptions ` , a ` Context ` is required to specify
404- where the harmonized color resources will be applied. You can optionally pass in
405- an array of resource ids for the color resources you'd like to harmonize, a
412+ We've provided the ` HarmonizedColors ` and ` HarmonizedColorsOptions ` classes in
413+ the ` com.google.android.material.color ` package for color resources
414+ harmonization. ` HarmonizedColorsOptions.Builder ` is a Builder class and to
415+ construct a ` HarmonizedColorsOptions ` . You can optionally pass in an array of
416+ resource ids for the color resources you'd like to harmonize, a
406417` HarmonizedColorAttributes ` object and/or the color attribute to harmonize with:
407418
408419```
409420HarmonizedColorsOptions options =
410- new HarmonizedColorsOptions.Builder(activity )
421+ new HarmonizedColorsOptions.Builder()
411422 .setColorResourceIds(colorResources)
412423 .setColorAttributes(HarmonizedColorAttributes.create(attributes))
413424 .setColorAttributeToHarmonizeWith(colorAttributeResId)
414425 .build();
415426```
416427
428+ In the ` HarmonizedColorsOptions ` class, we also provided a convenience method
429+ ` createMaterialDefaults() ` , with Error colors being harmonized by default.
430+
431+ ```
432+ HarmonizedColorsOptions options = HarmonizedColorsOptions.createMaterialDefaults();
433+ HarmonizedColors.applyToContextIfAvailable(context, options);
434+ ```
435+
436+ If you need to harmonize color resources at runtime to a context and use the
437+ harmonized color resources in xml, call:
438+
439+ ```
440+ HarmonizedColors.applyToContextIfAvailable(context, harmonizedColorsOptions);
441+ ```
442+
417443To return a new ` Context ` with color resources being harmonized, call:
418444
419445```
420- HarmonizedColors.wrapContextIfAvailable(harmonizedColorsOptions);
446+ HarmonizedColors.wrapContextIfAvailable(context, harmonizedColorsOptions);
421447```
422448
423449##### ` HarmonizedColorAttributes `
@@ -430,14 +456,63 @@ Static Factory Methods | Descr
430456
431457If the first static factory method is used, the color resource's id and value of
432458the attribute will be resolved at runtime and the color resources will be
433- harmonized. If you're concerned about accidentally overwriting color resources,
434- the second method should be used. In this method, instead of the color resource
435- that the attributes are pointing to being harmonized directly, resources value
436- in the theme overlay will be replaced instead.
459+ harmonized.
460+
461+ ** Note:** The way we harmonize color attributes is by looking up the color
462+ resource the attribute points to, and harmonizing the color resource directly.
463+ If you are looking to harmonize only color resources, in most cases when
464+ constructing ` HarmonizedColorsOptions ` , the
465+ ` setColorResourceIds(colorResources) ` method should be enough.
466+
467+ If you're concerned about accidentally overwriting color resources, the second
468+ static factory method should be used. In this method, instead of the color
469+ resource that the color attribute is pointing to in the main theme/context being
470+ harmonized directly, the color resources pointed by the color attributes after
471+ the theme overlay is applied will be harmonized. In the theme overlay, the color
472+ resources pointed by the color attributes are dummy values, to avoid color
473+ resources that the color attributs are pointing to in the main theme/context be
474+ overridden.
475+
476+ Here is an example of how we harmonize Error colors with theme overlay, to avoid
477+ accidentally overriding the resources from the main theme/context. We have an
478+ array of color attributes defined as:
479+
480+ ```
481+ private static final int[] HARMONIZED_MATERIAL_ATTRIBUTES =
482+ new int[] {
483+ R.attr.colorError,
484+ R.attr.colorOnError,
485+ R.attr.colorErrorContainer,
486+ R.attr.colorOnErrorContainer
487+ };
488+ ```
489+
490+ And a theme overlay defined as:
491+
492+ ```
493+ <style name="ThemeOverlay.Material3.HarmonizedColors" parent="">
494+ <item name="colorError">@color/material_harmonized_color_error</item>
495+ <item name="colorOnError">@color/material_harmonized_color_on_error</item>
496+ <item name="colorErrorContainer">@color/material_harmonized_color_error_container</item>
497+ <item name="colorOnErrorContainer">@color/material_harmonized_color_on_error_container</item>
498+ </style>
499+ ```
500+
501+ With this theme overlay, instead of directly overwriting the resources that
502+ ` colorError ` , ` colorOnError ` , ` colorErrorContainer ` , and ` colorOnErrorContainer `
503+ point to in the main theme/context, we would:
504+
505+ 1 . look up the resource values in the ` Context ` themed by the theme overlay
506+ 2 . retrieve the harmonized resources with Primary
507+ 3 . override ` @color/material_harmonized_color_error ` ,
508+ ` @color/material_harmonized_color_on_error ` , etc. with the harmonized colors
509+
510+ That way the Error roles in the theme overlay would point to harmonized
511+ resources.
437512
438- If you would like to harmonize additional color attributes along with the
439- attributes in ` HarmonizedColorAttributes.createMaterialDefaults() ` ,
440- the ` HarmonizedColorAttributes ` would look like:
513+ If you would like to harmonize additional color attributes along with
514+ harmonizing Error roles by default, the ` HarmonizedColorAttributes ` would look
515+ like:
441516
442517```
443518HarmonizedColorAttributes.create(
@@ -462,10 +537,11 @@ DynamicColorsOptions dynamicColorOptions =
462537 ...
463538 .setOnAppliedCallback(
464539 activity ->
465- HarmonizedColors.applyIfAvailable(
466- HarmonizedColorsOptions.createMaterialDefaults(activity)))
540+ HarmonizedColors.applyToContextIfAvailable(
541+ activity,
542+ HarmonizedColorsOptions.createMaterialDefaults()))
467543 .build()
468- DynamicColors.applyIfAvailable( dynamicColorOptions);
544+ DynamicColors.applyToActivityIfAvailable(activity, dynamicColorOptions);
469545```
470546
471547For color ressources harmonization in a fragment/view, you would use the context
@@ -478,10 +554,12 @@ harmonization:
478554Context newContext = DynamicColors.wrapContextIfAvailable(getContext());
479555
480556HarmonizedColorsOptions options =
481- new HarmonizedColorsOptions.Builder(newContext )
557+ new HarmonizedColorsOptions.Builder()
482558 .setColorResources(colorResources)
483559 .build();
484- HarmonizedColors.wrapContextIfAvailable(options);
560+ Context harmonizedContext = HarmonizedColors.wrapContextIfAvailable(newContext, options);
561+ // Usage example with the new harmonizedContext.
562+ MaterialColors.getColor(harmonizedContext, R.attr.customColor, -1);
485563```
486564
487565** Note:** This is only supported for API 30 and above.
0 commit comments