# ColorContrast

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

*** ** * ** ***


```
public class ColorContrast
```

<br />

*** ** * ** ***

Utility for applying contrast colors to application/activities.

Please note that if you are already using dynamic colors, contrast will be applied automatically on Android U+. This is only needed if you have a branded or custom theme and want to support contrast.

## Summary

| ### Public methods |
|---|---|
| `static void` | `https://developer.android.com/reference/com/google/android/material/color/ColorContrast#applyToActivitiesIfAvailable(Application,com.google.android.material.color.ColorContrastOptions)( Application application, https://developer.android.com/reference/com/google/android/material/color/ColorContrastOptions colorContrastOptions )` Applies contrast to all activities by registering a ActivityLifecycleCallbacks to your application. |
| `static void` | `https://developer.android.com/reference/com/google/android/material/color/ColorContrast#applyToActivityIfAvailable(Activity,com.google.android.material.color.ColorContrastOptions)( Activity activity, https://developer.android.com/reference/com/google/android/material/color/ColorContrastOptions colorContrastOptions )` Applies contrast to the given activity. |
| `static boolean` | `https://developer.android.com/reference/com/google/android/material/color/ColorContrast#isContrastAvailable()()` Returns `true` if contrast control is available on the current SDK level. |
| `static Context` | `https://developer.android.com/reference/com/google/android/material/color/ColorContrast#wrapContextIfAvailable(Context,com.google.android.material.color.ColorContrastOptions)( Context context, https://developer.android.com/reference/com/google/android/material/color/ColorContrastOptions colorContrastOptions )` Wraps the given context with the theme overlay where color resources are updated. |

## Public methods

### applyToActivitiesIfAvailable

```
public static void applyToActivitiesIfAvailable(
    Application application,
    ColorContrastOptions colorContrastOptions
)
```

Applies contrast to all activities 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();
    ColorContrast.applyToActivitiesIfAvailable(this);
  }
}
```

This method will try to apply a theme overlay in every activity's onActivityPreCreated callback.

| Parameters |
|---|---|
| `Application application` | The target application. |
| `https://developer.android.com/reference/com/google/android/material/color/ColorContrastOptions colorContrastOptions` | The color contrast options object that specifies the theme overlay resource IDs for medium and high contrast mode. |

### applyToActivityIfAvailable

```
public static void applyToActivityIfAvailable(
    Activity activity,
    ColorContrastOptions colorContrastOptions
)
```

Applies contrast to the given activity.

Note that this method does not guarantee the consistency of contrast throughout the app. If you want contrast to be updated automatically when a different contrast level is selected in the system, please use #applyToActivitiesIfAvailable(Application, ColorContrastOptions).

| Parameters |
|---|---|
| `Activity activity` | The target activity. |
| `https://developer.android.com/reference/com/google/android/material/color/ColorContrastOptions colorContrastOptions` | The color contrast options object that specifies the theme overlay resource IDs for medium and high contrast mode. |

### isContrastAvailable

```
public static boolean isContrastAvailable()
```

Returns `true` if contrast control is available on the current SDK level.

### wrapContextIfAvailable

```
public static Context wrapContextIfAvailable(
    Context context,
    ColorContrastOptions colorContrastOptions
)
```

Wraps the given context with the theme overlay where color resources are updated. The returned context can be used to create views with contrast support.

Note that this method does not guarantee the consistency of contrast throughout the app. If you want contrast to be updated automatically when a different contrast level is selected in the system, please use #applyToActivitiesIfAvailable(Application, ColorContrastOptions).

| Parameters |
|---|---|
| `Context context` | The target context. |
| `https://developer.android.com/reference/com/google/android/material/color/ColorContrastOptions colorContrastOptions` | The color contrast options object that specifies the theme overlay resource IDs for medium and high contrast mode. |