The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Custom Drawables

Many times, our artwork can simply be some PNG or JPEG files, perhaps with different variations in different resource directories by density.

Sometimes, though, we need something more.

In addition to supporting standard PNG and JPEG files, Android has a number of custom drawable resource formats — mostly written in XML — that handle specific scenarios.

For example, you may wish to customize “the background” of a Button, but a Button really has several different background images for different circumstances (normal, pressed, focused, disabled, etc.). Android has a certain type of drawable resource that aggregates other drawable resources, indicating which of those other resources should be used in different circumstances (e.g., for a normal button use X, for a disabled button use Y).

In this chapter, we will explore these non-traditional types of “drawables” and how you can use them within your apps.

Prerequisites

Understanding this chapter requires that you have read the core chapters, particularly the ones on basic resources, basic widgets, and vector drawables.

Having read the chapters on animators and legacy animations would be useful.

Where Do These Things Go?

All of the drawables described in this chapter, unless otherwise noted, are density-independent. Hence, they do not normally go in a density-dependent directories like res/drawable-hdpi/. However, that still leaves three possible candidates: res/drawable-nodpi/, res/drawable-anydpi/, and the unadorned res/drawable/.

nodpi: Fallback

A drawable in res/drawable-nodpi/ is valid for any screen density. However, if there is another drawable with the same base name in a density-specific directory, and the device running your app happens to have that screen density, the density-specific resource will be used. As a result, -nodpi becomes a fallback, to be used in cases where you do not have something specific for a density.

For example, suppose that we have res/drawable-nodpi/foo.xml and res/drawable-xxhdpi/foo.png. An -xxhdpi device would use the PNG; all other devices would use the XML.

anydpi: Takeover

A drawable in res/drawable-anydpi/ also is valid for any screen density. However, in this case, the -anydpi variant trumps any density-specific variant.

For example, suppose that we have res/drawable-anydpi/foo.xml and res/drawable-xxhdpi/foo.png. All devices would use the XML, even -xxhdpi devices.

For this reason, often you will see -anydpi used in conjunction with other qualifiers. A popular one will be -v21, to restrict the resources to be used on API Level 21+ devices.

No Qualifier: Just Say “WTF?”

res/drawable/ is a synonym for res/drawable-mdpi/, for backwards compatibility with really old Android apps, written before we had density-specific resources. Hence, res/drawable/ is not really an appropriate choice for density-independent drawables.

Alas, Android Studio may put some drawables here, for uncertain reasons.

So long as there are no other resources with the same basename, the choice made by Android Studio’s developers is unlikely to cause any harm.

ColorDrawable

The preview of this section was eaten by a grue.

AnimationDrawable

The preview of this section is being chased by zombies.

StateListDrawable

The preview of this section left for Hollywood to appear in a reality TV show.

ColorStateList

The preview of this section was the victim of a MITM ('Martian in the middle') attack.

LayerDrawable

The preview of this section was lost due to a rupture in the space-time continuum.

TransitionDrawable

The preview of this section was abducted by space aliens.

LevelListDrawable

The preview of this section may contain nuts.

ScaleDrawable and ClipDrawable

The preview of this section will not appear here for a while, due to a time machine mishap.

InsetDrawable

The preview of this section will not appear here for a while, due to a time machine mishap.

ShapeDrawable

The preview of this section was last seen in the Bermuda Triangle.

BitmapDrawable

The preview of this section was fed to a gremlin, after midnight.

Composite Drawables

The preview of this section was the victim of a MITM ('Martian in the middle') attack.

A Stitch In Time Saves Nine

The preview of this section was abducted by space aliens.