Raw Color
ID |
kotlin.android_raw_color |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Kotlin |
Tags |
android, maintainability |
Description
Flags Android XML attribute values that hard-code a hexadecimal color literal (#RRGGBB or #AARRGGBB) instead of referencing a color resource declared in res/values/colors.xml.
Rationale
Hard-coding colors in layouts prevents theme switching (light/dark mode), accessibility overrides, and global color refactors. Colors copied into every layout drift over time and are hard to keep consistent. Defining colors once as resources and referencing them via @color/… makes the palette discoverable, reusable, and theme-aware.
<!-- Bad: hard-coded color -->
<TextView android:textColor="#FF0000" android:text="Error" />
Attributes in the tools: namespace are skipped because they are design-time only and do not ship with the APK.
Remediation
Declare the color in res/values/colors.xml and reference it by name.
<!-- res/values/colors.xml -->
<resources>
<color name="error_text">#FF0000</color>
</resources>
<!-- Good: color resource reference -->
<TextView android:textColor="@color/error_text" android:text="Error" />