DynamicColors

public class DynamicColors
extends Object

java.lang.Object
   ↳ com.google.android.material.color.DynamicColors


Utility for applying dynamic colors to application/activities.

Summary

Nested classes

interface DynamicColors.OnAppliedCallback

The interface that provides a callback method after dynamic colors have been applied. 

interface DynamicColors.Precondition

The interface that provides a precondition to decide if dynamic colors should be applied. 

Public methods

static void applyIfAvailable(Activity activity)

This method is deprecated. Use applyToActivityIfAvailable(Activity) instead.

static void applyIfAvailable(Activity activity, int theme)

This method is deprecated. Use applyToActivityIfAvailable(Activity, DynamicColorsOptions) instead.

static void applyIfAvailable(Activity activity, DynamicColors.Precondition precondition)

This method is deprecated. Use applyToActivityIfAvailable(Activity, DynamicColorsOptions) instead.

static void applyToActivitiesIfAvailable(Application application, DynamicColors.Precondition precondition)

This method is deprecated. Use applyToActivitiesIfAvailable(Application, DynamicColorsOptions) instead.

static void applyToActivitiesIfAvailable(Application application, int theme, DynamicColors.Precondition precondition)

This method is deprecated. Use applyToActivitiesIfAvailable(Application, DynamicColorsOptions) instead.

static void applyToActivitiesIfAvailable(Application application, int theme)

This method is deprecated. Use applyToActivitiesIfAvailable(Application, DynamicColorsOptions) instead.

static void applyToActivitiesIfAvailable(Application application)

Applies dynamic colors to all activities with the theme overlay designated by the theme attribute dynamicColorThemeOverlay by registering a Application.ActivityLifecycleCallbacks to your application.

static void applyToActivitiesIfAvailable(Application application, DynamicColorsOptions dynamicColorsOptions)

Applies dynamic colors to all activities based on the provided DynamicColorsOptions, by registering a Application.ActivityLifecycleCallbacks to your application.

static void applyToActivityIfAvailable(Activity activity)

Applies dynamic colors to the given activity.

static void applyToActivityIfAvailable(Activity activity, DynamicColorsOptions dynamicColorsOptions)

Applies dynamic colors to the given activity with DynamicColorsOptions provided.

static boolean isDynamicColorAvailable()

Returns true if dynamic colors are available on the current SDK level.

static Context wrapContextIfAvailable(Context originalContext, int theme)

Wraps the given context with the given theme overlay.

static Context wrapContextIfAvailable(Context originalContext, DynamicColorsOptions dynamicColorsOptions)

Wraps the given context with the given theme overlay provided in DynamicColorsOptions.

static Context wrapContextIfAvailable(Context originalContext)

Wraps the given context with the theme overlay designated by the theme attribute dynamicColorThemeOverlay.

Inherited methods

Public methods

applyIfAvailable

public static void applyIfAvailable (Activity activity)

This method is deprecated.
Use applyToActivityIfAvailable(Activity) instead.

Applies dynamic colors to the given activity with the theme overlay designated by the theme attribute dynamicColorThemeOverlay.

Parameters
activity Activity: The target activity.

applyIfAvailable

public static void applyIfAvailable (Activity activity, 
                int theme)

This method is deprecated.
Use applyToActivityIfAvailable(Activity, DynamicColorsOptions) instead.

Applies dynamic colors to the given activity with the given theme overlay.

Parameters
activity Activity: The target activity.

theme int: The resource ID of the theme overlay that provides dynamic color definition.

applyIfAvailable

public static void applyIfAvailable (Activity activity, 
                DynamicColors.Precondition precondition)

This method is deprecated.
Use applyToActivityIfAvailable(Activity, DynamicColorsOptions) instead.

Applies dynamic colors to the given activity with the theme overlay designated by the theme attribute dynamicColorThemeOverlay according to the given precondition.

Parameters
activity Activity: The target activity.

precondition DynamicColors.Precondition: The precondition to decide if dynamic colors should be applied.

applyToActivitiesIfAvailable

public static void applyToActivitiesIfAvailable (Application application, 
                DynamicColors.Precondition precondition)

This method is deprecated.
Use applyToActivitiesIfAvailable(Application, DynamicColorsOptions) instead.

Applies dynamic colors to all activities with the theme overlay designated by the theme attribute dynamicColorThemeOverlay according to the given precondition by registering a Application.ActivityLifecycleCallbacks to your application.

Parameters
application Application: The target application.

precondition DynamicColors.Precondition: The precondition to decide if dynamic colors should be applied.

applyToActivitiesIfAvailable

public static void applyToActivitiesIfAvailable (Application application, 
                int theme, 
                DynamicColors.Precondition precondition)

This method is deprecated.
Use applyToActivitiesIfAvailable(Application, DynamicColorsOptions) instead.

Applies dynamic colors to all activities with the given theme overlay according to the given precondition by registering a Application.ActivityLifecycleCallbacks to your application.

Parameters
application Application: The target application.

theme int: The resource ID of the theme overlay that provides dynamic color definition.

precondition DynamicColors.Precondition: The precondition to decide if dynamic colors should be applied.

applyToActivitiesIfAvailable

public static void applyToActivitiesIfAvailable (Application application, 
                int theme)

This method is deprecated.
Use applyToActivitiesIfAvailable(Application, DynamicColorsOptions) instead.

Applies dynamic colors to all activities with the given theme overlay by registering a Application.ActivityLifecycleCallbacks to your application.

Parameters
application Application: The target application.

theme int: The resource ID of the theme overlay that provides dynamic color definition.

applyToActivitiesIfAvailable

public static void applyToActivitiesIfAvailable (Application application)

Applies dynamic colors to all activities with the theme overlay designated by the theme attribute dynamicColorThemeOverlay by registering a Application.ActivityLifecycleCallbacks to your application.

Parameters
application Application: The target application.

applyToActivitiesIfAvailable

public static void applyToActivitiesIfAvailable (Application application, 
                DynamicColorsOptions dynamicColorsOptions)

Applies dynamic colors to all activities based on the provided DynamicColorsOptions, by registering a Application.ActivityLifecycleCallbacks to your application.

A normal usage of this method should happen only once in Application.onCreate() or any methods that run before any of your activities are created. For example:

 public class YourApplication extends Application {
   @Override
   public void onCreate() {
     super.onCreate();
     DynamicColors.applyToActivitiesWithCallbacks(this);
   }
 }
 
This method will try to apply the given dynamic color theme overlay in every activity's Application.ActivityLifecycleCallbacks.onActivityPreCreated(Activity, Bundle) callback. Therefore, if you are applying any other theme overlays after that, you will need to be careful about not overriding the colors or you may lose the dynamic color support.

Parameters
application Application: The target application.

dynamicColorsOptions DynamicColorsOptions: The dynamic colors options object that specifies the theme resource ID, precondition to decide if dynamic colors should be applied and the callback function for after dynamic colors have been applied.

applyToActivityIfAvailable

public static void applyToActivityIfAvailable (Activity activity)

Applies dynamic colors to the given activity.

Parameters
activity Activity: The target activity.

applyToActivityIfAvailable

public static void applyToActivityIfAvailable (Activity activity, 
                DynamicColorsOptions dynamicColorsOptions)

Applies dynamic colors to the given activity with DynamicColorsOptions provided.

Parameters
activity Activity: The target activity.

dynamicColorsOptions DynamicColorsOptions: The dynamic colors options object that specifies the theme resource ID, precondition to decide if dynamic colors should be applied and the callback function for after dynamic colors have been applied.

isDynamicColorAvailable

public static boolean isDynamicColorAvailable ()

Returns true if dynamic colors are available on the current SDK level.

Returns
boolean

wrapContextIfAvailable

public static Context wrapContextIfAvailable (Context originalContext, 
                int theme)

Wraps the given context with the given theme overlay. The returned context can be used to create views with dynamic color support.

If dynamic color support is not available, the original context will be returned.

Parameters
originalContext Context: The original context.

theme int: The resource ID of the theme overlay that provides dynamic color definition.

Returns
Context

wrapContextIfAvailable

public static Context wrapContextIfAvailable (Context originalContext, 
                DynamicColorsOptions dynamicColorsOptions)

Wraps the given context with the given theme overlay provided in DynamicColorsOptions. The returned context can be used to create views with dynamic color support.

If dynamic color support is not available, the original context will be returned.

Parameters
originalContext Context: The original context.

dynamicColorsOptions DynamicColorsOptions: The dynamic colors options object that specifies the theme resource ID, seed color for content-based dynamic colors.

Returns
Context

wrapContextIfAvailable

public static Context wrapContextIfAvailable (Context originalContext)

Wraps the given context with the theme overlay designated by the theme attribute dynamicColorThemeOverlay. The returned context can be used to create views with dynamic color support.

If dynamic color support or the dynamic color theme overlay is not available, the original context will be returned.

Parameters
originalContext Context: The original context.

Returns
Context