ColorContrast

public class ColorContrast
extends Object

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


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 applyToActivitiesIfAvailable(Application application, ColorContrastOptions colorContrastOptions)

Applies contrast to all activities by registering a Application.ActivityLifecycleCallbacks to your application.

static void applyToActivityIfAvailable(Activity activity, ColorContrastOptions colorContrastOptions)

Applies contrast to the given activity.

static boolean isContrastAvailable()

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

static Context wrapContextIfAvailable(Context context, ColorContrastOptions colorContrastOptions)

Wraps the given context with the theme overlay where color resources are updated.

Inherited methods

Public methods

applyToActivitiesIfAvailable

public static void applyToActivitiesIfAvailable (Application application, 
                ColorContrastOptions colorContrastOptions)

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

This method will try to apply a theme overlay in every activity's Application.ActivityLifecycleCallbacks.onActivityPreCreated(Activity, Bundle) callback.

Parameters
application Application: The target application.

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.

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.

Returns
boolean

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.

colorContrastOptions ColorContrastOptions: The color contrast options object that specifies the theme overlay resource IDs for medium and high contrast mode.

Returns
Context