The Guide to Color Gradients in Android Studio

Code on the Rocks
5 min readMay 22, 2020

Adjusting the background color of your activities and fragments is one of the easiest ways to add flair to your mobile apps. A strong color palette can make a bad app okay and a good app great if applied with artistic tenderness. In this article, I’m going to explore one component of the tender artist’s toolkit: the gradient. I’ll also go one step further and illustrate how a gradient can be animated on screen automatically or with respect to some other value.

Gradient Drawables

Drawables in Android are essentially a set of instructions that tell the screen how to render itself. Rather than import multiple sizes of the same JPEG to accomodate various screen dimensions, you can instead create a single drawable that tells the device what to draw and where to draw it. Drawables can only be so complex but the space-saving benefit of using them can make up for their simplicity.

When designing a drawable, you can define shapes, colors, line thicknesses, and yes, gradients. In fact a GradientDrawable is simply a shape drawable with a gradient defined. You can take a deeper dive into modifying <shape> elements another time but for now, just know that these are created by:

  1. Right-clicking on the drawable folder in your file tree
  2. Selecting “New -> Drawable Resource File”
  3. Changing the root element to “shape”

--

--