Drawable resources

A drawable resource is a general concept for a graphic that can be drawn to the screen and that you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon. There are several types of drawables:

Bitmap file
A bitmap graphic file (PNG, WEBP, JPG, or GIF). Creates a BitmapDrawable.
Nine-patch file
A PNG file with stretchable regions to let images resize based on content (.9.png). Creates a NinePatchDrawable.
Layer list
A drawable that manages an array of other drawables. These are drawn in array order, so the element with the largest index is drawn on top. Creates a LayerDrawable.
State list
An XML file that references different bitmap graphics for different states—for example, to use a different image when a button is tapped. Creates a StateListDrawable.
Level list
An XML file that defines a drawable that manages a number of alternate drawables, each assigned a maximum numerical value. Creates a LevelListDrawable.
Transition drawable
An XML file that defines a drawable that can cross-fade between two drawable resources. Creates a TransitionDrawable.
Inset drawable
An XML file that defines a drawable that insets another drawable by a specified distance. This is useful when a view needs a background drawable that is smaller than the view's actual bounds.
Clip drawable
An XML file that defines a drawable that clips another drawable based on this drawable's current level value. Creates a ClipDrawable.
Scale drawable
An XML file that defines a drawable that changes the size of another drawable based on its current level value. Creates a ScaleDrawable
Shape drawable.
An XML file that defines a geometric shape, including colors and gradients. Creates a GradientDrawable.

For information about how to create an AnimationDrawable, see the Animation resources document.

Note: A color resource can also be used as a drawable in XML. For example, when creating a state list drawable, you can reference a color resource for the android:drawable attribute (android:drawable="@color/green").

Bitmap

A bitmap image. Android supports bitmap files in the following formats: PNG (preferred), WEBP (preferred, requires API level 17 or higher), JPG (acceptable), GIF (discouraged).

You can reference a bitmap file directly, using the filename as the resource ID, or create an alias resource ID in XML.

Note: Bitmap files might be automatically optimized with lossless image compression by the aapt tool during the build process. For example, a true-color PNG that doesn't require more than 256 colors might be converted to an 8-bit PNG with a color palette. This results in an image of equal quality that requires less memory.

So, be aware that the image binaries placed in this directory can change during the build. If you plan to read an image as a bit stream to convert it to a bitmap, put your images in the res/raw/ folder instead, where they aren't optimized.

Bitmap file

A bitmap file is a PNG, WEBP, JPG, or GIF file. Android creates a Drawable resource for any of these files when you save them in the res/drawable/ directory.

file location:
res/drawable/filename.png (.png, .webp, .jpg, or .gif)
The filename is the resource ID
compiled resource datatype:
Resource pointer to a BitmapDrawable
resource reference:
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
example:
With an image saved at res/drawable/myimage.png, this layout XML applies the image to a view:
<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/myimage" />

The following application code retrieves the image as a Drawable:

Kotlin

val drawable: Drawable? = ResourcesCompat.getDrawable(resources, R.drawable