Stay organized with collections
Save and categorize content based on your preferences.
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
Inherited methods |
From class
java.lang.Object
Object
|
clone()
|
boolean
|
equals(Object arg0)
|
void
|
finalize()
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
final
void
|
wait()
|
|
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.
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.
|
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# ColorContrast\n\nSummary: [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \n\nColorContrast\n=============\n\n\n`\npublic\n\n\nclass\nColorContrast\n`\n\n\n`\n\nextends Object\n\n\n`\n\n`\n\n\n`\n\n|---|-------------------------------------------------|\n| java.lang.Object ||\n| ↳ | com.google.android.material.color.ColorContrast |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nUtility for applying contrast colors to application/activities.\n\nPlease note that if you are already using dynamic colors, contrast will be applied\nautomatically on Android U+. This is only needed if you have a branded or custom theme and want\nto support contrast.\n\nSummary\n-------\n\n| ### Public methods ||\n|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` static void` | ` `[applyToActivitiesIfAvailable](/reference/com/google/android/material/color/ColorContrast#applyToActivitiesIfAvailable(android.app.Application,%20com.google.android.material.color.ColorContrastOptions))`(Application application, `[ColorContrastOptions](/reference/com/google/android/material/color/ColorContrastOptions)` colorContrastOptions) ` Applies contrast to all activities by registering a [Application.ActivityLifecycleCallbacks](/reference/android/app/Application.ActivityLifecycleCallbacks) to your application. |\n| ` static void` | ` `[applyToActivityIfAvailable](/reference/com/google/android/material/color/ColorContrast#applyToActivityIfAvailable(android.app.Activity,%20com.google.android.material.color.ColorContrastOptions))`(Activity activity, `[ColorContrastOptions](/reference/com/google/android/material/color/ColorContrastOptions)` colorContrastOptions) ` Applies contrast to the given activity. |\n| ` static boolean` | ` `[isContrastAvailable](/reference/com/google/android/material/color/ColorContrast#isContrastAvailable())`() ` Returns `true` if contrast control is available on the current SDK level. |\n| ` static Context` | ` `[wrapContextIfAvailable](/reference/com/google/android/material/color/ColorContrast#wrapContextIfAvailable(android.content.Context,%20com.google.android.material.color.ColorContrastOptions))`(Context context, `[ColorContrastOptions](/reference/com/google/android/material/color/ColorContrastOptions)` colorContrastOptions) ` Wraps the given context with the theme overlay where color resources are updated. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From class ` java.lang.Object ` |-------------------|-------------------------------| | ` Object` | ` clone() ` | | ` boolean` | ` equals(Object arg0) ` | | ` void` | ` finalize() ` | | ` final Class\u003c?\u003e` | ` getClass() ` | | ` int` | ` hashCode() ` | | ` final void` | ` notify() ` | | ` final void` | ` notifyAll() ` | | ` String` | ` toString() ` | | ` final void` | ` wait(long arg0, int arg1) ` | | ` final void` | ` wait(long arg0) ` | | ` final void` | ` wait() ` | ||\n\nPublic methods\n--------------\n\n### applyToActivitiesIfAvailable\n\n```\npublic static void applyToActivitiesIfAvailable (Application application, \n ColorContrastOptions colorContrastOptions)\n```\n\nApplies contrast to all activities by registering a [Application.ActivityLifecycleCallbacks](/reference/android/app/Application.ActivityLifecycleCallbacks) to your\napplication.\n\nA normal usage of this method should happen only once in [Application.onCreate()](/reference/android/app/Application#onCreate()) or\nany methods that run before any of your activities are created. For example:\n\n```\n public class YourApplication extends Application {\n @Override\n public void onCreate() {\n super.onCreate();\n ColorContrast.applyToActivitiesIfAvailable(this);\n }\n }\n \n```\n\nThis method will try to apply a theme overlay in every activity's [Application.ActivityLifecycleCallbacks.onActivityPreCreated(Activity, Bundle)](/reference/android/app/Application.ActivityLifecycleCallbacks#onActivityPreCreated(android.app.Activity,%20android.os.Bundle)) callback.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| `application` | `Application`: The target application. \u003cbr /\u003e |\n| `colorContrastOptions` | `ColorContrastOptions`: The color contrast options object that specifies the theme overlay resource IDs for medium and high contrast mode. \u003cbr /\u003e |\n\n### applyToActivityIfAvailable\n\n```\npublic static void applyToActivityIfAvailable (Activity activity, \n ColorContrastOptions colorContrastOptions)\n```\n\nApplies contrast to the given activity.\n\nNote that this method does not guarantee the consistency of contrast throughout the app. If\nyou want contrast to be updated automatically when a different contrast level is selected in\nthe system, please use #applyToActivitiesIfAvailable(Application, ColorContrastOptions).\n\n\u003cbr /\u003e\n\n| Parameters ||\n|------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| `activity` | `Activity`: The target activity. \u003cbr /\u003e |\n| `colorContrastOptions` | `ColorContrastOptions`: The color contrast options object that specifies the theme overlay resource IDs for medium and high contrast mode. \u003cbr /\u003e |\n\n### isContrastAvailable\n\n```\npublic static boolean isContrastAvailable ()\n```\n\nReturns `true` if contrast control is available on the current SDK level.\n\n\u003cbr /\u003e\n\n| Returns ||\n|-----------|--------|\n| `boolean` | \u003cbr /\u003e |\n\n### wrapContextIfAvailable\n\n```\npublic static Context wrapContextIfAvailable (Context context, \n ColorContrastOptions colorContrastOptions)\n```\n\nWraps the given context with the theme overlay where color resources are updated. The returned\ncontext can be used to create views with contrast support.\n\nNote that this method does not guarantee the consistency of contrast throughout the app. If\nyou want contrast to be updated automatically when a different contrast level is selected in\nthe system, please use #applyToActivitiesIfAvailable(Application, ColorContrastOptions).\n\n\u003cbr /\u003e\n\n| Parameters ||\n|------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| `context` | `Context`: The target context. \u003cbr /\u003e |\n| `colorContrastOptions` | `ColorContrastOptions`: The color contrast options object that specifies the theme overlay resource IDs for medium and high contrast mode. \u003cbr /\u003e |\n\n| Returns ||\n|-----------|--------|\n| `Context` | \u003cbr /\u003e |"]]