Member-only story
The Guide to Color Gradients in Android Studio
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:
- Right-clicking on the drawable folder in your file tree
- Selecting “New -> Drawable Resource File”
- Changing the root element to “shape”