aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: a7f26c6a7bcdd1fb5002206495fc7fc7d370a182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Lint check for hardcoded colors

## What is this lint check for

This check detects whether hardcoded colors have been added in a CL.

Starting in Android O, multiple device themes will exist on devices, enabling
changing the look and feel of all system UI. In order to make that effort
possible, colors in component that uses this check must be specified using
theme attributes, rather than hardcoded colors. Otherwise, when the theme
changes, this UI will not update properly.

## Examples of hardcoded colors

### Color files

```xml
<!-- File: res/values/colors.xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="hardcoded_color">#FFFFFF</color>
</resources>
```

### Layout files

```xml
<!-- File: res/layout/my_layout.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:textColor="#FF000000" />
```

Or

```xml
<!-- File: res/layout/my_layout.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:textColor="@color/hardcoded_color" />
```

### Style files

```xml
<!-- File: res/values/styles.xml -->
<style name="MyStyle">
    <item name="android:textColor">#ff3c3c3c</item>
</style>
```

## How to fix it

### Use attributes in theming as much as possible

Here are some tips to choose the colors.

#### Choose colors for text

Use color attributes specified in the theme. Some examples:

1.  `textColorPrimary`
2.  `textColorSecondary`
3.  `colorAccent`

#### Choose colors for icon

For `vector drawable`, please use full opacity color as `fillColor` and `tint`
it with theme attribute.
[example](https://googleplex-android-review.git.corp.google.com/#/c/1606392/2/packages/SettingsLib/res/drawable/ic_menu.xml)

#### Others

Please check the following table for more available options.

| Attribute |   Description |
|---|---|
| colorAccent | Bright complement to the primary branding color. By default, this is the color applied to framework controls (via colorControlActivated). |
| colorForeground | Color for foreground imagery.  Most text and image colors will be based on some alpha of colorForeground. |
| colorBackground | Color of background imagery, ex. full-screen windows.	|
| colorBackgroundFloating | Color of background imagery for floating components, ex. dialogs, popups, and cards. |
| colorPrimary | The primary branding color for the app. This is the color applied to the action bar background. |
| 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). |
| colorError | Color used for error states and things that need to be drawn to the users attention. |
| textColorPrimary (colorPrimaryText) | Is now constructed out of colorForeground and primaryContentAlpha. |
| textColorSecondary (colorSecondaryText) | Is now constructed out of colorForeground and primaryContentAlpha. |

## How to bypass it

**We strongly discourage bypassing color lint check**.

However, if you need to bypass the check, please update the `baseline.xml` by running following
command in package root folder(i.e. package/app/Settings/)

```
lint --check HardCodedColor --xml color-check-baseline.xml .
```

After update the `baseline.xml`, your hardcoded color will be ignored in check. Please submit the
new `baseline.xml` within your cl.

## Contact us

1.  For help to remove hardcoded colors or report issue in color check, please contact
jackqdyulei@google.com