# DynamicColors

[View Source](https://github.com/material-components/material-components-android/blob/1.14.0/lib/java/com/google/android/material/color/DynamicColors.java)

*** ** * ** ***


```
public class DynamicColors
```

<br />

*** ** * ** ***

Utility for applying dynamic colors to application/activities.

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/com/google/android/material/color/DynamicColors.OnAppliedCallback` The interface that provides a callback method after dynamic colors have been applied. |
| `public interface https://developer.android.com/reference/com/google/android/material/color/DynamicColors.Precondition` The interface that provides a precondition to decide if dynamic colors should be applied. |

| ### Public methods |
|---|---|
| `static void` | `[applyIfAvailable](https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyIfAvailable(Activity))(Activity activity)` **This method is deprecated.** Use `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivityIfAvailable(Activity)` instead. <br /> |
| `static void` | `[applyIfAvailable](https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyIfAvailable(Activity,com.google.android.material.color.DynamicColors.Precondition))( Activity activity, https://developer.android.com/reference/com/google/android/material/color/DynamicColors.Precondition precondition )` **This method is deprecated.** Use `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivityIfAvailable(Activity,com.google.android.material.color.DynamicColorsOptions)` instead. <br /> |
| `static void` | `[applyIfAvailable](https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyIfAvailable(Activity,int))(Activity activity, int theme)` **This method is deprecated.** Use `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivityIfAvailable(Activity,com.google.android.material.color.DynamicColorsOptions)` instead. <br /> |
| `static void` | `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application)(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` | `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,com.google.android.material.color.DynamicColorsOptions)( Application application, https://developer.android.com/reference/com/google/android/material/color/DynamicColorsOptions dynamicColorsOptions )` Applies dynamic colors to all activities based on the provided `https://developer.android.com/reference/com/google/android/material/color/DynamicColorsOptions`, by registering a ActivityLifecycleCallbacks to your application. |
| `static void` | `[applyToActivitiesIfAvailable](https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,com.google.android.material.color.DynamicColors.Precondition))( Application application, https://developer.android.com/reference/com/google/android/material/color/DynamicColors.Precondition precondition )` **This method is deprecated.** Use `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,com.google.android.material.color.DynamicColorsOptions)` instead. <br /> |
| `static void` | `[applyToActivitiesIfAvailable](https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,int))(Application application, int theme)` **This method is deprecated.** Use `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,com.google.android.material.color.DynamicColorsOptions)` instead. <br /> |
| `static void` | `[applyToActivitiesIfAvailable](https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,int,com.google.android.material.color.DynamicColors.Precondition))( Application application, int theme, https://developer.android.com/reference/com/google/android/material/color/DynamicColors.Precondition precondition )` **This method is deprecated.** Use `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,com.google.android.material.color.DynamicColorsOptions)` instead. <br /> |
| `static void` | `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivityIfAvailable(Activity)(Activity activity)` Applies dynamic colors to the given activity. |
| `static void` | `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivityIfAvailable(Activity,com.google.android.material.color.DynamicColorsOptions)( Activity activity, https://developer.android.com/reference/com/google/android/material/color/DynamicColorsOptions dynamicColorsOptions )` Applies dynamic colors to the given activity with `https://developer.android.com/reference/com/google/android/material/color/DynamicColorsOptions` provided. |
| `static boolean` | `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#isDynamicColorAvailable()()` Returns `true` if dynamic colors are available on the current SDK level. |
| `static Context` | `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#wrapContextIfAvailable(Context)(Context originalContext)` Wraps the given context with the theme overlay designated by the theme attribute ` dynamicColorThemeOverlay`. |
| `static Context` | `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#wrapContextIfAvailable(Context,com.google.android.material.color.DynamicColorsOptions)( Context originalContext, https://developer.android.com/reference/com/google/android/material/color/DynamicColorsOptions dynamicColorsOptions )` Wraps the given context with the given theme overlay provided in `https://developer.android.com/reference/com/google/android/material/color/DynamicColorsOptions`. |
| `static Context` | `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#wrapContextIfAvailable(Context,int)(Context originalContext, int theme)` Wraps the given context with the given theme overlay. |

## Public methods

### applyIfAvailable

```
public static void [applyIfAvailable](https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyIfAvailable(Activity))(Activity activity)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#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](https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyIfAvailable(Activity,com.google.android.material.color.DynamicColors.Precondition))(
    Activity activity,
    DynamicColors.Precondition precondition
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivityIfAvailable(Activity,com.google.android.material.color.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. |
| `https://developer.android.com/reference/com/google/android/material/color/DynamicColors.Precondition precondition` | The precondition to decide if dynamic colors should be applied. |

### applyIfAvailable

```
public static void [applyIfAvailable](https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyIfAvailable(Activity,int))(Activity activity, int theme)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivityIfAvailable(Activity,com.google.android.material.color.DynamicColorsOptions)` instead.

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 |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,com.google.android.material.color.DynamicColorsOptions)` | 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 `https://developer.android.com/reference/com/google/android/material/color/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. |
| `https://developer.android.com/reference/com/google/android/material/color/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](https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,com.google.android.material.color.DynamicColors.Precondition))(
    Application application,
    DynamicColors.Precondition precondition
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,com.google.android.material.color.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 ActivityLifecycleCallbacks to your application.

| Parameters |
|---|---|
| `Application application` | The target application. |
| `https://developer.android.com/reference/com/google/android/material/color/DynamicColors.Precondition precondition` | The precondition to decide if dynamic colors should be applied. |

### applyToActivitiesIfAvailable

```
public static void [applyToActivitiesIfAvailable](https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,int))(Application application, int theme)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,com.google.android.material.color.DynamicColorsOptions)` instead.

Applies dynamic colors to all activities with the given theme overlay 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. |

### applyToActivitiesIfAvailable

```
public static void [applyToActivitiesIfAvailable](https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,int,com.google.android.material.color.DynamicColors.Precondition))(
    Application application,
    int theme,
    DynamicColors.Precondition precondition
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivitiesIfAvailable(Application,com.google.android.material.color.DynamicColorsOptions)` instead.

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. |
| `https://developer.android.com/reference/com/google/android/material/color/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. |

| See also |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/color/DynamicColors#applyToActivityIfAvailable(Activity,com.google.android.material.color.DynamicColorsOptions)` |   |

### applyToActivityIfAvailable

```
public static void applyToActivityIfAvailable(
    Activity activity,
    DynamicColorsOptions dynamicColorsOptions
)
```

Applies dynamic colors to the given activity with `https://developer.android.com/reference/com/google/android/material/color/DynamicColorsOptions` provided.

| Parameters |
|---|---|
| `Activity activity` | The target activity. |
| `https://developer.android.com/reference/com/google/android/material/color/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 `https://developer.android.com/reference/com/google/android/material/color/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. |
| `https://developer.android.com/reference/com/google/android/material/color/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. |