How to set background color of a View
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Setting the background color of a View is one of the most basic Android UI operations. You can do it declaratively in XML layout files, programmatically in Java or Kotlin, or through styles and themes for consistent app-wide appearance. The key methods are the android:background XML attribute and the setBackgroundColor() / setBackgroundResource() methods in code. Understanding the difference between color integers, color resources, and drawables prevents common bugs.
Setting Background Color in XML
The android:background attribute accepts hex colors (#AARRGGBB), color resources (@color/name), or drawable resources (@drawable/name). Hex colors with 8 digits include alpha — #FF prefix means fully opaque.
Setting Background Color Programmatically (Kotlin)
Setting Background Color Programmatically (Java)
Use ContextCompat.getColor() instead of the deprecated getResources().getColor() for backward compatibility with older Android versions.
Defining Color Resources
Color resources centralize color definitions. When you change @color/primary, every View referencing it updates automatically.
Using Styles and Themes
Styles extract repeated View attributes into reusable definitions. Apply a style to set background color along with other properties consistently across your app.
Rounded Corners and Shapes
A shape drawable gives you rounded corners, borders, and gradients — all things a flat color cannot provide.
Dynamic Color Changes
ValueAnimator with ArgbEvaluator smoothly transitions between two colors. This is useful for selection states, hover effects, or theme transitions.
Jetpack Compose
In Jetpack Compose, use the background() modifier with a Color value. Compose handles recomposition automatically when the color changes.
Common Pitfalls
- Using getResources().getColor() without theme: The single-argument
getColor(int)is deprecated. UseContextCompat.getColor(context, R.color.name)which handles theme attributes correctly on all API levels. - setBackgroundColor() removes drawable properties: Calling
setBackgroundColor()replaces any existing background drawable (rounded corners, borders). If you need to change the color of a shape drawable, get the drawable and mutate it instead. - Forgetting alpha in hex colors:
#6200EEis interpreted as#FF6200EE(fully opaque) in XML. ButColor.parseColor("#6200EE")in code also defaults to opaque. For transparency, use 8-digit hex like#806200EE(50% opacity). - Null context in ContextCompat.getColor(): Passing a null context crashes. In Fragments, use
requireContext()instead ofgetContext()to fail fast rather than getting a NullPointerException later. - Dark mode not handled: Hardcoding hex colors ignores dark mode. Use theme attributes (
?attr/colorSurface) or color resources withres/values-night/colors.xmlvariants so backgrounds adapt to the system theme.
Summary
- Use
android:backgroundin XML for static backgrounds (hex colors, resources, or drawables) - Use
setBackgroundColor()in code withContextCompat.getColor()for backward compatibility - Use
setBackgroundResource()to apply shape drawables with rounded corners and borders - Define colors in
res/values/colors.xmland reference with@color/namefor maintainability - Use styles and themes for consistent backgrounds across multiple Views
- In Jetpack Compose, use the
background()modifier withColorvalues

