A drawable resource is a general concept for a graphic that canbe drawn to the screen and which you can retrieve with APIs such asgetDrawable(int)
or apply to another XML resource withattributes such as android:drawable
andandroid:icon
. There are several different types ofdrawables:
.png
,.jpg
,or
.gif
). Creates a
BitmapDrawable
.
.9.png
). Creates a
NinePatchDrawable
.
LayerDrawable
.
StateListDrawable
.
LevelListDrawable
.
TransitionDrawable
.
ClipDrawable
.
ScaleDrawable
ShapeDrawable
.
Also see the Animation Resource document for how to create an AnimationDrawable
.
Note: A color resource can also be used as a drawable in XML. Forexample,when creating a state list drawable,you can reference a color resource for theandroid:drawable
attribute(android:drawable="@color/green"
).
A bitmap image. Android supports bitmap files in a threeformats: .png
(preferred),.jpg
(acceptable),.gif
(discouraged).
You can reference a bitmap file directly,using the filename asthe resource ID,or create an alias resource ID in XML.
Note: Bitmap files may beautomatically optimized with lossless image compression by theaapt tool. For example,a true-color PNG that does not requiremore than 256 colors may be converted to an 8-bit PNG with a colorpalette. This will result in an image of equal quality but whichrequires less memory. So be aware that the image binaries placed inthis directory can change during the build. If you plan on readingan image as a bit stream in order to convert it to a bitmap,putyour images in the res/raw/
folder instead,where theywill not be optimized.
A bitmap file is a .png
,or.gif
file. Android creates a Drawable
resource for any of these files when you savethem in the res/drawable/
directory.
res/drawable/filename.png
(
.png
,or
.gif
)
BitmapDrawable
.
R.drawable.filename
@[package:]drawable/filename
res/drawable/myimage.png
,this layout XML applies the image to a View:
<ImageViewandroid:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/myimage" />
The following application code retrieves the image as aDrawable
:
Resources res = ; Drawable drawable = res.(R.drawable.myimage); getResources()getDrawable
An XML bitmap is a resource defined in XML that points to abitmap file. The effect is an alias for a raw bitmap file. The XMLcan specify additional properties for the bitmap such as ditheringand tiling.
Note: You can use a<bitmap>
element as achild of an <item>
element. For example,when creating a state list or layer list,you can exclude the android:drawable
attribute from an <item>
element and nest a<bitmap>
inside it thatdefines the drawable item.
res/drawable/filename.xml
BitmapDrawable
.
R.drawable.filename
@[package:]drawable/filename
<?xml version="1.0" encoding="utf-8"?> <bitmapxmlns: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:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
<bitmap>
attributes:
xmlns:android
"http://schemas.android.com/apk/res/android"
. This isrequired only if the
<bitmap>
is the rootelement—it is not needed when the
<bitmap>
is nestedinside an
<item>
.
android:src
android:antialias
android:dither
android:filter
android:gravity
Must be one or more (separated by '|') of the following constantvalues:
Value | Description |
---|---|
top |
Put the object at the top of its container,not changing itssize. |
bottom |
Put the object at the bottom of its container,not changing itssize. |
left |
Put the object at the left edge of its container,not changingits size. |
right |
Put the object at the right edge of its container,not changingits size. |
center_vertical |
Place object in the vertical center of its container,notchanging its size. |
fill_vertical |
Grow the vertical size of the object if needed so it completelyfills its container. |
center_horizontal |
Place object in the horizontal center of its container,notchanging its size. |
fill_horizontal |
Grow the horizontal size of the object if needed so itcompletely fills its container. |
center |
Place the object in the center of its container in both thevertical and horizontal axis,not changing its size. |
fill |
Grow the horizontal and vertical size of the object if neededso it completely fills its container. This is the default. |
clip_vertical |
Additional option that can be set to have the top and/or bottomedges of the child clipped to its container's bounds. The clip isbased on the vertical gravity: a top gravity clips the bottom edge,a bottom gravity clips the top edge,and neither clips bothedges. |
clip_horizontal |
Additional option that can be set to have the left and/or rightedges of the child clipped to its container's bounds. The clip isbased on the horizontal gravity: a left gravity clips the rightedge,a right gravity clips the left edge,and neither clips bothedges. |
android:tileMode
Must be one of the following constant values:
Value | Description |
---|---|
disabled |
Do not tile the bitmap. This is the default value. |
clamp |
Replicates the edge color if the shader draws outside of itsoriginal 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 NinePatch
is a PNG image in which you can definestretchable regions that Android scales when content within theView exceeds the normal image bounds. You typically assign thistype of image as the background of a View that has at least onedimension set to "wrap_content"
,and when the Viewgrows to accomodate the content,the Nine-Patch image is alsoscaled to match the size of the View. An example use of aNine-Patch image is the background used by Android's standardButton
widget,which must stretch to accommodate thetext (or image) inside the button.
Same as with a normal bitmap,you can reference a Nine-Patch file directly or from aresource defined by XML.
For a complete discussion about how to create a Nine-Patch filewith stretchable regions,see the 2D Graphics document.
res/drawable/filename.9.png
NinePatchDrawable
.
R.drawable.filename
@[package:]drawable/filename
res/drawable/myninepatch.9.png
,this layout XMLapplies the Nine-Patch to a View:
<Buttonandroid:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/myninepatch" />
An XML Nine-Patch is a resource defined in XML that points to aNine-Patch file. The XML can specify dithering for the image.
res/drawable/filename.xml
NinePatchDrawable
.
R.drawable.filename
@[package:]drawable/filename
<?xml version="1.0" encoding="utf-8"?> <nine-patchxmlns:android="http://schemas.android.com/apk/res/android" android:src="@[package:]drawable/drawable_resource" android:dither=["true" | "false"] />
<nine-patch>
attributes:
xmlns:android
"http://schemas.android.com/apk/res/android"
.
android:src
android:dither
<?xml version="1.0" encoding="utf-8"?> <nine-patch xmlns:android="http://schemas.android.com/apk/res/android"android:src="@drawable/myninepatch" android:dither="false" />
A LayerDrawable
is a drawable object that manages an arrayof other drawables. Each drawable in the list is drawn in the orderof the list—the last drawable in the list is drawn on top.
Each drawable is represented by an<item>
element inside asingle <layer-list>
element.
res/drawable/filename.xml
LayerDrawable
.
R.drawable.filename
@[package:]drawable/filename
<?xml version="1.0" encoding="utf-8"?> <layer-listxmlns: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>
<layer-list>
<item>
elements.
attributes:
xmlns:android
"http://schemas.android.com/apk/res/android"
.
<item>
<selector>
element.Accepts child
<bitmap>
elements.
attributes:
android:drawable
android:id
"@+id/name"
. The plus symbol indicates thatthis should be created as a new ID. You can use this identifier toretrieve and modify the drawable with
View.findViewById()
or
Activity.findViewById()
.
android:top
android:right
android:bottom
android:left
All drawable items are scaled to fit the size of the containingView,by default. Thus,placing your images in a layer list atdifferent positions might increase the size of the View and someimages scale as appropriate. To avoid scaling items in the list,use a <bitmap>
elementinside the <item>
element to specify the drawable and define the gravity to somethingthat does not scale,such as "center"
. For example,the following <item>
defines an item that scales to fit its container View:
<item android:drawable="@drawable/image" />
To avoid scaling,the following example uses a<bitmap>
element withcentered gravity:
<item><bitmap android:src="@drawable/image" android:gravity="center" /> </item>
res/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 this example uses a nested<bitmap>
element todefine the drawable resource for each item with a "center" gravity.This ensures that none of the images are scaled to fit the size ofthe container,due to resizing caused by the offset images.
This layout XML applies the drawable to a View:
<ImageViewandroid:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/layers" />
The result is a stack of increasingly offset images:
A StateListDrawable
is a drawable object defined in XMLthat uses a several different images to represent the same graphic,depending on the state of the object. For example,a Button
widget can exist in one of several differentstates (pressed,focused,or niether) and,using a state listdrawable,you can provide a different background image for eachstate.
You can describe the state list in an XML file. Each graphic isrepresented by an <item>
element inside a single<selector>
element. Each<item>
uses varIoUsattributes to describe the state in which it should be used as thegraphic for the drawable.
During each state change,the state list is traversed top tobottom and the first item that matches the current state isused—the selection is not based on the "best match," butsimply the first item that meets the minimum criteria of thestate.
res/drawable/filename.xml
StateListDrawable
.
R.drawable.filename
@[package:]drawable/filename
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"android:constantSize=["true" | "false"] android:dither=["true" | "false"] android:variablePadding=["true" | "false"] > <item android:drawable="@[package:]drawable/drawable_resource" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_window_focused=["true" | "false"] /> </selector>
<selector>
<item>
elements.
attributes:
xmlns:android
"http://schemas.android.com/apk/res/android"
.
android:constantSize
android:dither
android:variablePadding
<item>
<selector>
element.
attributes:
android:drawable
android:state_pressed
android:state_focused
android:state_selected
android:state_checkable
android:state_checked
android:state_enabled
android:state_window_focused
Note: Remember that Androidapplies the first item in the state list that matches the currentstate of the object. So,if the first item in the list containsnone of the state attributes above,then it is applied every time,which is why your default value should always be last (asdemonstrated in the following example).
res/drawable/button.xml
:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <!-- focused --> <item android:drawable="@drawable/button_normal" /> <!-- default --> </selector>
This layout XML applies the state list drawable to a Button:
<Buttonandroid:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/button" />
A Drawable that manages a number of alternate Drawables,eachassigned a maximum numerical value. Setting the level value of thedrawable with setLevel()
loads the drawable resource in the level listthat has a android:maxLevel
value greater than orequal to the value passed to the method.
res/drawable/filename.xml
LevelListDrawable
.
R.drawable.filename
@[package:]drawable/filename
<?xml version="1.0" encoding="utf-8"?> <level-listxmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/drawable_resource" android:maxLevel="integer" android:minLevel="integer" /> </level-list>
<level-list>
<item>
elements.
attributes:
xmlns:android
"http://schemas.android.com/apk/res/android"
.
<item>
attributes:
android:drawable
android:maxLevel
android:minLevel
<?xml version="1.0" encoding="utf-8"?> <level-list xmlns:android="http://schemas.android.com/apk/res/android" ><item android:drawable="@drawable/status_off" android:maxLevel="0" /> <item android:drawable="@drawable/status_on" android:maxLevel="1" /> </level-list>
Once this is applied to a View
,the level can be changed with setLevel()
or setImageLevel()
.
A TransitionDrawable
is a drawable object that cancross-fade between the two drawable resources.
Each drawable is represented by an<item>
element inside asingle <transition>
element. No more than two items are supported. To transitionforward,call startTransition()
. To transition backward,callreverseTransition()
.
res/drawable/filename.xml
TransitionDrawable
.
R.drawable.filename
@[package:]drawable/filename
<?xml version="1.0" encoding="utf-8"?> <transition 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" /> </transition>
<transition>
<item>
elements.
attributes:
xmlns:android
"http://schemas.android.com/apk/res/android"
.
<item>
<transition>
element.Accepts child
<bitmap>
elements.
attributes:
android:drawable
android:id
"@+id/name"
. The plus symbol indicates thatthis should be created as a new ID. You can use this identifier toretrieve and modify the drawable with
View.findViewById()
or
Activity.findViewById()
.
android:top
android:right
android:bottom
android:left
res/drawable/transition.xml
:
<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/on" /> <item android:drawable="@drawable/off" /> </transition>
This layout XML applies the drawable to a View:
<ImageButtonandroid:id="@+id/button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/transition" />
And the following code performs a 500ms transition from thefirst item to the second:
ImageButton button = (ImageButton) findViewById(R.id.button); TransitionDrawable drawable = (TransitionDrawable) button.getDrawable(); drawable.startTransition(500);
A drawable defined in XML that insets another drawable by aspecified distance. This is useful when a View needs a backgroundthat is smaller than the View's actual bounds.
res/drawable/filename.xml
InsetDrawable
.
R.drawable.filename
@[package:]drawable/filename
<?xml version="1.0" encoding="utf-8"?> <insetxmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/drawable_resource" android:insetTop="dimension" android:insetRight="dimension" android:insetBottom="dimension" android:insetLeft="dimension" />
<inset>
attributes:
xmlns:android
"http://schemas.android.com/apk/res/android"
.
android:drawable
android:insetTop
android:insetRight
android:insetBottom
android:insetLeft
<?xml version="1.0" encoding="utf-8"?> <inset xmlns:android="http://schemas.android.com/apk/res/android"android:drawable="@drawable/background" android:insetTop="10dp" android:insetLeft="10dp" />
A drawable defined in XML that clips another drawable based onthis Drawable's current level. You can control how much the childdrawable gets clipped in width and height based on the level,aswell as a gravity to control where it is placed in its overallcontainer. Most often used to implement things like progressbars.
res/drawable/filename.xml
ClipDrawable
.
R.drawable.filename
@[package:]drawable/filename
<?xml version="1.0" encoding="utf-8"?> <clipxmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/drawable_resource" android:clipOrientation=["horizontal" | "vertical"] android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" | "fill_vertical" | "center_horizontal" | "fill_horizontal" | "center" | "fill" | "clip_vertical" | "clip_horizontal"] />
<clip>
attributes:
xmlns:android
"http://schemas.android.com/apk/res/android"
.
android:drawable
android:clipOrientation
Must be one of the following constant values:
Value | Description |
---|---|
horizontal |
Clip the drawable horizontally. |
vertical |
Clip the drawable vertically. |
android:gravity
Must be one or more (separated by '|') of the following constantvalues:
Value | Description |
---|---|
top |
Put the object at the top of its container,not changing itssize. When clipOrientation is "vertical" ,clipping occurs at the bottom of the drawable. |
bottom |
Put the object at the bottom of its container,clipping occurs at the top of the drawable. |
left |
Put the object at the left edge of its container,not changingits size. This is the default. When clipOrientation is"horizontal" ,clipping occurs at the right side of thedrawable. This is the default. |
right |
Put the object at the right edge of its container,not changingits size. When clipOrientation is"horizontal" ,clipping occurs at the left side of thedrawable. |
center_vertical |
Place object in the vertical center of its container,notchanging its size. Clipping behaves the same as when gravity is"center" . |
fill_vertical |
Grow the vertical size of the object if needed so it completelyfills its container. When clipOrientation is"vertical" ,no clipping occurs because the drawablefills the vertical space (unless the drawable level is 0,in whichcase it's not visible). |
center_horizontal |
Place object in the horizontal center of its container,notchanging its size. Clipping behaves the same as when gravity is"center" . |
fill_horizontal |
Grow the horizontal size of the object if needed so itcompletely fills its container. When clipOrientation is "horizontal" ,no clipping occurs because thedrawable fills the horizontal space (unless the drawable level is0,in which case it's not visible). |
center |
Place the object in the center of its container in both thevertical and horizontal axis,not changing its size. WhenclipOrientation is "horizontal" ,clippingoccurs on the left and right. When clipOrientation is"vertical" ,clipping occurs on the top andbottom. |
fill |
Grow the horizontal and vertical size of the object if neededso it completely fills its container. No clipping occurs becausethe drawable fills the horizontal and vertical space (unless thedrawable level is 0,in which case it's not visible). |
clip_vertical |
Additional option that can be set to have the top and/or bottomedges of the child clipped to its container's bounds. The clip isbased on the vertical gravity: a top gravity clips the bottom edge,and neither clips bothedges. |
res/drawable/clip.xml
:
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android"android:drawable="@drawable/android" android:clipOrientation="horizontal" android:gravity="left" /> </clip>
The following layout XML applies the clip drawable to aView:
<ImageViewandroid:id="@+id/image" android:background="@drawable/clip" android:layout_height="wrap_content" android:layout_width="wrap_content" />
The following code gets the drawable and increases the amount ofclipping in order to progressively reveal the image:
ImageView imageview = (ImageView) findViewById(R.id.image); ClipDrawable drawable = (ClipDrawable) imageview.getDrawable(); drawable.setLevel(drawable.getLevel() + 1000);
Increasing the level reduces the amount of clipping and slowlyreveals the image. Here it is at a level of 7000:
Note: The default level is 0,which is fully clipped so the image is not visible. When the levelis 10,000,the image is not clipped and completely visible.
A drawable defined in XML that changes the size of anotherdrawable based on its current level.
res/drawable/filename.xml
ScaleDrawable
.
R.drawable.filename
@[package:]drawable/filename
<?xml version="1.0" encoding="utf-8"?> <scalexmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/drawable_resource" android:scaleGravity=["top" | "bottom" | "left" | "right" | "center_vertical" | "fill_vertical" | "center_horizontal" | "fill_horizontal" | "center" | "fill" | "clip_vertical" | "clip_horizontal"] android:scaleHeight="percentage" android:scaleWidth="percentage" />
<scale>
attributes:
xmlns:android
"http://schemas.android.com/apk/res/android"
.
android:drawable
android:scaleGravity
Must be one or more (separated by '|') of the following constantvalues:
Value | Description |
---|---|
top |
Put the object at the top of its container,not changingits size. This is the default. |
right |
Put the object at the right edge of its container,not changing its size. |
fill |
Grow the horizontal and vertical size of the object if neededso it completely fills its container. |
clip_vertical |
Additional option that can be set to have the top and/or bottomedges of the child clipped to its container's bounds. The clip isbased on the vertical gravity: a top gravity clips the bottom edge,and neither clips bothedges. |
android:scaleHeight
android:scaleWidth
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android"android:drawable="@drawable/@R_301_395@" android:scaleGravity="center_vertical|center_horizontal" android:scaleHeight="80%" android:scaleWidth="80%" />
This is a generic shape defined in XML.
res/drawable/filename.xml
ShapeDrawable
.
R.drawable.filename
@[package:]drawable/filename
<?xml version="1.0" encoding="utf-8"?> <shapexmlns: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:usesLevel=["true" | "false"] /> <padding android:left="integer" android:top="integer" android:right="integer" android:bottom="integer" /> <size android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer" /> <solid android:color="color" /> <stroke android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer" /> </shape>
<shape>
attributes:
xmlns:android
"http://schemas.android.com/apk/res/android"
.
android:shape
Value | Desciption |
---|---|
"rectangle" |
A rectangle that fills the containing View. This is the defaultshape. |
"oval" |
An oval shape that fits the dimensions of the containingView. |
"line" |
A horizontal line that spans the width of the containing View.This shape requires the<stroke> element todefine the width of the line. |
"ring" |
A ring shape. |
The following attributes are used only whenandroid:shape="ring"
:
android:innerRadius
android:innerRadiusRatio
android:innerRadiusRatio="5"
,then the inner radiusequals the ring's width divided by 5. This value is overridden by
android:innerRadius
. Default value is 9.
android:thickness
android:thicknessRatio
android:thicknessRatio="2"
,then the thickness equalsthe ring's width divided by 2. This value is overridden by
android:innerRadius
. Default value is 3.
android:useLevel
LevelListDrawable
. This should normally be "false" oryour shape may not appear.
<corners>
attributes:
android:radius
android:topLeftRadius
android:topRightRadius
android:bottomLeftRadius
android:bottomRightRadius
Note: Every corner must(initially) be provided a corner radius greater than 1,or else nocorners are rounded. If you want specific corners to notbe rounded,a work-around is to use android:radius
toset a default corner radius greater than 1,but then override eachand every corner with the values you really want,providing zero("0dp") where you don't want rounded corners.
<gradient>
attributes:
android:angle
android:centerX
android:type="linear"
.
android:centerY
android:type="linear"
.
android:centerColor
android:endColor
android:gradientRadius
android:type="radial"
.
android:startColor
android: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:useLevel
LevelListDrawable
.
<padding>
attributes:
android:left
android:top
android:right
android:bottom
<size>
attributes:
android:height
android:width
Note: The shape scales to the sizeof the container View proportionate to the dimensions defined here,by default. When you use the shape in an ImageView
,you can restrict scaling by setting theandroid:scaleType
to "center"
.
<solid>
attributes:
android:color
<stroke>
attributes:
android:width
android:color
android:dashGap
android:dashWidth
is set.
android:dashWidth
android:dashGap
is set.
res/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:
<TextViewandroid:background="@drawable/gradient_Box" android:layout_height="wrap_content" android:layout_width="wrap_content" />
This application code gets the shape drawable and applies it toa View:
Resources res = ; Drawable shape = res. (R.drawable.gradient_Box); TextView tv = (TextView)findViewByID(R.id.textview); tv.setBackground(shape); getResources()getDrawable