README.md
1# Lint check for hardcoded colors
2
3## What is this lint check for
4
5This check detects whether hardcoded colors have been added in a CL.
6
7Starting in Android O, multiple device themes will exist on devices, enabling
8changing the look and feel of all system UI. In order to make that effort
9possible, colors in component that uses this check must be specified using
10theme attributes, rather than hardcoded colors. Otherwise, when the theme
11changes, this UI will not update properly.
12
13## Examples of hardcoded colors
14
15### Color files
16
17```xml
18<!-- File: res/values/colors.xml -->
19<?xml version="1.0" encoding="utf-8"?>
20<resources>
21 <color name="hardcoded_color">#FFFFFF</color>
22</resources>
23```
24
25### Layout files
26
27```xml
28<!-- File: res/layout/my_layout.xml -->
29<?xml version="1.0" encoding="utf-8"?>
30<TextView
31 android:textColor="#FF000000" />
32```
33
34Or
35
36```xml
37<!-- File: res/layout/my_layout.xml -->
38<?xml version="1.0" encoding="utf-8"?>
39<TextView
40 android:textColor="@color/hardcoded_color" />
41```
42
43### Style files
44
45```xml
46<!-- File: res/values/styles.xml -->
47<style name="MyStyle">
48 <item name="android:textColor">#ff3c3c3c</item>
49</style>
50```
51
52## How to fix it
53
54### Use attributes in theming as much as possible
55
56Here are some tips to choose the colors.
57
58#### Choose colors for text
59
60Use color attributes specified in the theme. Some examples:
61
621. `textColorPrimary`
632. `textColorSecondary`
643. `colorAccent`
65
66#### Choose colors for icon
67
68For `vector drawable`, please use full opacity color as `fillColor` and `tint`
69it with theme attribute.
70[example](https://googleplex-android-review.git.corp.google.com/#/c/1606392/2/packages/SettingsLib/res/drawable/ic_menu.xml)
71
72#### Others
73
74Please check the following table for more available options.
75
76| Attribute | Description |
77|---|---|
78| colorAccent | Bright complement to the primary branding color. By default, this is the color applied to framework controls (via colorControlActivated). |
79| colorForeground | Color for foreground imagery. Most text and image colors will be based on some alpha of colorForeground. |
80| colorBackground | Color of background imagery, ex. full-screen windows. |
81| colorBackgroundFloating | Color of background imagery for floating components, ex. dialogs, popups, and cards. |
82| colorPrimary | The primary branding color for the app. This is the color applied to the action bar background. |
83| colorPrimaryDark | Dark variant of the primary branding color. By default, this is the color applied to the status bar (via statusBarColor) and navigation bar (via navigationBarColor). |
84| colorError | Color used for error states and things that need to be drawn to the users attention. |
85| textColorPrimary (colorPrimaryText) | Is now constructed out of colorForeground and primaryContentAlpha. |
86| textColorSecondary (colorSecondaryText) | Is now constructed out of colorForeground and primaryContentAlpha. |
87
88## How to bypass it
89
90**We strongly discourage bypassing color lint check**.
91
92However, if you need to bypass the check, please update the `baseline.xml` by running following
93command in package root folder(i.e. package/app/Settings/)
94
95```
96export ANDROID_LINT_JARS=$(gettop)/prebuilts/checkcolor/checkcolor.jar
97lint --check HardCodedColor --xml color-check-baseline.xml .
98```
99
100After update the `baseline.xml`, your hardcoded color will be ignored in check. Please submit the
101new `baseline.xml` within your cl.
102
103## Contact us
104
1051. For help to remove hardcoded colors or report issue in color check, please contact
106[email protected]
107
108