A 'Drawable' resource is a general concept for a graphic which can be drawn.
Every Drawable is stored as individual files in one of the res/drawable folders.Typically you would store bitmaps for different resolutions in the -mdpi, -hdpi, -xhdpi, -xxhdpi subfolders of res/drawable.The ADT project creation wizard creates these folders by default. If these bitmaps are provided in different folder, the Android system selects the correct one automatically based on the device configuration.
In addition to graphical files, Android supports XML drawables and 9-patch graphics. XML drawables are used to describe shapes (color, border, gradient), state, transitions and more.
9-patch graphics are used to define which part of a graphic should be stretched if the view which uses this graphic is larger than the graphic.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/hello"
android:text="@string/hello_world" />
In code you can also assign drawables to views. Most views accept an resource ID as input parameter. For example the following code shows how to set a drawables as background to an ImageView.
ImageView imageView = (ImageView) findViewById(R.id.image); imageView.setImageResource(R.drawable.hello);
res/drawable/myshape.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#FFFFFFFF" />
<gradient
android:endColor="#DDBBBBBB"
android:startColor="#DD777777"
android:angle="90" />
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
You could assign that drawable to the background property of your layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/myshape"
android:orientation="vertical">
.
.
.
.
.
.
.
.
.
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/button_checked"
android:state_checked="true" />
<item android:drawable="@drawable/button_default" />
</selector>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#4C4C43"
android:endColor="#B8B894"
android:angle="270" />
</shape>
</item>
</selector>


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#4C4C43"
android:centerColor="#B8B894"
android:endColor="#4C4C43"
android:angle="270" />
</shape>
</item>
</selector>
In below layout I have used these gradient drawable as background resource of Button.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="2dp"
android:color="#F44336" />
<padding
android:bottom="7dp"
android:left="7dp"
android:right="7dp"
android:top="7dp" />
<corners android:radius="25dp" />
<solid android:color="#9C27B0" />
</shape>
In main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:padding="25dp" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/solid_bg"
android:padding="10dp"
android:textSize="18sp"
android:textStyle="bold"
android:text="SUBMIT"
android:textColor="#FFF" />
</LinearLayout>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:dashGap="10px"
android:dashWidth="10px"
android:width="1dp"
android:color="#F00" />
</shape>
res/drawable/gradient_lines.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="0"
android:centerColor="#97CF4D"
android:endColor="#000000"
android:startColor="#000000" />
</shape>
res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:padding="25dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Registration"
android:textColor="#FFF"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#FF0000FF" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:background="@drawable/dotted_line" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="@drawable/gradient_lines" />
<View
android:layout_width="2dp"
android:layout_height="100dp"
android:background="#FF0000FF" />
</LinearLayout>
| Type | Description |
|---|---|
| Bitmap File | A bitmap graphic file (.png, .jpg, or .gif). |
| Nine-Patch File | A PNG file with stretchable regions to allow image resizing based on content (.9.png). |
| 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 be drawn on top. |
| State List | An XML file that references different bitmap graphics for different states (for example, to use a different image when a button is pressed). |
| Level List | An XML file that defines a drawable that manages a number of alternate Drawables, each assigned a maximum numerical value. |
| Transition Drawable | An XML file that defines a drawable that can cross-fade between two drawable resources. |
| 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 drawble 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. |
| Scale Drawable | An XML file that defines a drawable that changes the size of another Drawable based on its current level value. |
| Shape Drawable | An XML file that defines a geometric shape, including colors and gradients. |
A bitmap image. Android supports bitmap files in three formats: .png (preferred), .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.
res/drawable/ directory.res/drawable/filename.png (.png, .jpg, or .gif)(The filename is used as the resource ID.)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" />
Resources res = getResources(); Drawable drawable = res.getDrawable(R.drawable.myimage);
res/drawable/filename.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@[package:]drawable/drawable_resource"
android:antialias=["true" | "false"]
android:dither=["true" | "false"]
android:filter=["true" | "false"]
android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
"fill_vertical" | "center_horizontal" | "fill_horizontal" |
"center" | "fill" | "clip_vertical" | "clip_horizontal"]
android:mipMap=["true" | "false"]
android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
android:srcandroid:antialiasandroid:ditherandroid:filterandroid:gravity| Value | Description |
|---|---|
| top | Put the object at the top of its container, not changing its size. |
| bottom | Put the object at the bottom of its container, not changing its size. |
| left | Put the object at the left edge of its container, not changing its size. |
| right | Put the object at the right edge of its container, not changing its size. |
| center_vertical | Place object in the vertical center of its container, not changing its size. |
| fill_vertical | Grow the vertical size of the object if needed so it completely fills its container. |
| center_horizontal | Place object in the horizontal center of its container, not changing its size. |
| fill_horizontal | Grow the horizontal size of the object if needed so it completely fills its container. |
| center | Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. |
| fill | Grow the horizontal and vertical size of the object if needed so it completely fills its container. This is the default. |
| clip_vertical | Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip is based on the vertical gravity: a top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges. |
| clip_horizontal | Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip is based on the horizontal gravity: a left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges. |
android:mipMapandroid:tileMode| Value | Description |
|---|---|
| disabled | Do not tile the bitmap. This is the default value. |
| clamp | Replicates the edge color if the shader draws outside of its original bounds. |
| repeat | Repeats the shader's image horizontally and vertically. |
| mirror | Repeats the shader's image horizontally and vertically, alternating mirror images so that adjacent images always seam. |
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/icon"
android:tileMode="repeat" />
A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.
Each drawable is represented by an <item> element inside a single <layer-list> element.
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:id="@[+][package:]id/resource_name"
android:top="dimension"
android:right="dimension"
android:bottom="dimension"
android:left="dimension" />
</layer-list>
<item>android:drawableandroid:idandroid:topandroid:rightandroid:bottomandroid:left<item android:drawable="@drawable/image" />To avoid scaling, the following example uses a <bitmap> element with centered gravity:
<item>
<bitmap android:src="@drawable/image"
android:gravity="center" />
</item>
EXAMPLEres/drawable/layers.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@drawable/android_red"
android:gravity="center" />
</item>
<item android:top="10dp" android:left="10dp">
<bitmap android:src="@drawable/android_green"
android:gravity="center" />
</item>
<item android:top="20dp" android:left="20dp">
<bitmap android:src="@drawable/android_blue"
android:gravity="center" />
</item>
</layer-list>
Notice that in this example,none of the images are scaled to fit the size of the container.
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/layers" />
res/drawable/filename.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size
android:width="integer"
android:height="integer" />
<solid
android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
</shape>
<shape>android:shape| Value | Description |
|---|---|
| rectangle | A rectangle that fills the containing View. This is the default shape. |
| oval | An oval shape that fits the dimensions of the containing View. |
| line | A horizontal line that spans the width of the containing View. This shape requires the <stroke> element to define the width of the line. |
| ring | A ring shape. |
android:shape="ring":android:innerRadiusandroid:innerRadiusRatioandroid:thicknessandroid:thicknessRatioandroid:useLevelandroid:radius
Dimension. The radius for all corners.
android:topLeftRadius
Dimension. The radius for the top-left corner.
android:topRightRadius
Dimension. The radius for the bottom-left corner.
android:bottomRightRadius
Dimension. The radius for the bottom-right corner.
<gradient>android:angleandroid:centerXandroid:centerYandroid:centerColorandroid:endColorandroid:gradientRadiusandroid:startColorandroid:type| Value | Description |
|---|---|
| linear | A linear gradient. This is the default. |
| radial | A radial gradient. The start color is the center color. |
| sweep | A sweeping line gradient. |
android:useLevelandroid:leftandroid:topandroid:rightandroid:bottomandroid:heightandroid:widthandroid:colorandroid:widthandroid:colorandroid:dashGapandroid:dashWidthres/drawable/gradient_box.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
This layout XML applies the shape drawable to a View:
<TextView
android:background="@drawable/gradient_box"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
A NinePatchDrawable graphic is a stretchable bitmap image, which Android will automatically resize to accommodate the contents of the View in which you have placed it as the background.
An example use of a NinePatch is the backgrounds used by standard Android buttons - buttons must stretch to accommodate strings of various lengths.
It must be saved with the extension .9.png, and saved into the res/drawable/ directory of your project.

res/drawable/my_button_background.9.png
<Button id="@+id/tiny"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="Tiny"
android:textSize="8sp"
android:background="@drawable/my_button_background"/>
<Button id="@+id/big"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:text="Biiiiiiig text!"
android:textSize="30sp"
android:background="@drawable/my_button_background"/>
Note that the width and height are set to wrap_content to make the button fit neatly around the text.


