Home | History | Annotate | Download | only in checkcolor
      1 # Lint check for hardcoded colors
      2 
      3 ## What is this lint check for
      4 
      5 This check detects whether hardcoded colors have been added in a CL.
      6 
      7 Starting in Android O, multiple device themes will exist on devices, enabling
      8 changing the look and feel of all system UI. In order to make that effort
      9 possible, colors in component that uses this check must be specified using
     10 theme attributes, rather than hardcoded colors. Otherwise, when the theme
     11 changes, 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 
     34 Or
     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 
     56 Here are some tips to choose the colors.
     57 
     58 #### Choose colors for text
     59 
     60 Use color attributes specified in the theme. Some examples:
     61 
     62 1.  `textColorPrimary`
     63 2.  `textColorSecondary`
     64 3.  `colorAccent`
     65 
     66 #### Choose colors for icon
     67 
     68 For `vector drawable`, please use full opacity color as `fillColor` and `tint`
     69 it 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 
     74 Please 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 
     92 However, if you need to bypass the check, please update the `baseline.xml` by running following
     93 command in package root folder(i.e. package/app/Settings/)
     94 
     95 ```
     96 export ANDROID_LINT_JARS=$(gettop)/prebuilts/checkcolor/checkcolor.jar
     97 lint --check HardCodedColor --xml color-check-baseline.xml .
     98 ```
     99 
    100 After update the `baseline.xml`, your hardcoded color will be ignored in check. Please submit the
    101 new `baseline.xml` within your cl.
    102 
    103 ## Contact us
    104 
    105 1.  For help to remove hardcoded colors or report issue in color check, please contact
    106 jackqdyulei (a] google.com
    107 
    108