DynamicColors

public class DynamicColors


Utility for applying dynamic colors to application/activities.

Summary

Nested types

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

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 instead.

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

This method is deprecated.

Use applyToActivityIfAvailable instead.

static void
applyIfAvailable(Activity activity, int theme)

This method is deprecated.

Use applyToActivityIfAvailable 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 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 ActivityLifecycleCallbacks to your application.

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

This method is deprecated.

Use applyToActivitiesIfAvailable instead.

static void
applyToActivitiesIfAvailable(Application application, int theme)

This method is deprecated.

Use applyToActivitiesIfAvailable instead.

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

This method is deprecated.

Use applyToActivitiesIfAvailable instead.

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

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

static Context
wrapContextIfAvailable(Context originalContext)

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

static Context
wrapContextIfAvailable(
    Context originalContext,
    DynamicColorsOptions dynamicColorsOptions
)

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

static Context
wrapContextIfAvailable(Context originalContext, int theme)

Wraps the given context with the given theme overlay.

Public methods

applyIfAvailable

public static void applyIfAvailable(Activity activity)

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,
    DynamicColors.Precondition precondition
)

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.

DynamicColors.Precondition precondition

The precondition to decide if dynamic colors should be applied.

applyIfAvailable

public static void applyIfAvailable(Activity activity, int theme)

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

Parameters
Activity activity

The target activity.

int theme

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 ActivityLifecycleCallbacks to your application.

Parameters
Application application

The target application.

See also
applyToActivitiesIfAvailable

for more detailed info and examples.

applyToActivitiesIfAvailable

public static void applyToActivitiesIfAvailable(
    Application application,
    DynamicColorsOptions dynamicColorsOptions
)

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

A normal usage of this method should happen only once in 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 onActivityPreCreated 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.

applyToActivitiesIfAvailable

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

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

Parameters
Application application

The target application.

DynamicColors.Precondition precondition

The precondition to decide if dynamic colors should be applied.

applyToActivitiesIfAvailable

public static void applyToActivitiesIfAvailable(Application application, int theme)

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

Parameters
Application application

The target application.

int theme

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

applyToActivitiesIfAvailable

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

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

Parameters
Application application

The target application.

int theme

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

DynamicColors.Precondition precondition

The precondition to decide if dynamic colors should be 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.

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
Context originalContext

The original 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
Context originalContext

The original context.

DynamicColorsOptions dynamicColorsOptions

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

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
Context originalContext

The original context.

int theme

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