drawable-resource.jd revision c6cb8a78d03cda44a49a990b4d4153560bee7420
1page.title=Drawable Resources 2parent.title=Resource Types 3parent.link=available-resources.html 4@jd:body 5 6<div id="qv-wrapper"> 7 <div id="qv"> 8 <h2>See also</h2> 9 <ol> 10 <li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html">2D Graphics</a></li> 11 </ol> 12 </div> 13</div> 14 15<p>A drawable resource is a general concept for a graphic that you 16can retrieve with {@link android.content.res.Resources#getDrawable(int)} 17and draw on the screen. There are several different types of drawables:</p> 18<dl> 19 <dt><a href="#Bitmap">Bitmap File</a><dt> 20 <dd>A bitmap graphic file ({@code .png}, {@code .jpg}, or {@code .gif}). 21 A {@link android.graphics.drawable.BitmapDrawable}.</dd> 22 <dt><a href="#NinePatch">Nine-Patch File</a></dt> 23 <dd>A PNG file with stretchable regions to allow image resizing based on content ({@code 24.9.png}). A {@link android.graphics.drawable.NinePatchDrawable}.</dd> 25<!-- <dt><a href="#BitmapAlias">Bitmap Alias</a><dt> 26 <dd>An alias for a drawable.</dd> --> 27 <dt><a href="#StateList">State List</a></dt> 28 <dd>An XML file that references different bitmap graphics 29 for different states (for example, to use a different image when a button is pressed). 30 A {@link android.graphics.drawable.StateListDrawable}.</dd> 31 <dt><a href="#Color">Color</a></dt> 32 <dd>A resource defined in XML that specifies a rectangle of color, with 33 optionally rounded corners. A {@link android.graphics.drawable.PaintDrawable}.</dd> 34 <dt><a href="#Shape">Shape</a></dt> 35 <dd>An XML file that defines a geometric shape, including colors and gradients. 36 A {@link android.graphics.drawable.ShapeDrawable}.</dd> 37</dl> 38 39<p>Documentation for the {@link android.graphics.drawable.AnimationDrawable} resource 40is in the <a href="animation-resource.html">Animation Resource</a> document.</p> 41 42<h2 id="Bitmap">Bitmap File</h2> 43 44<p>A basic bitmap image. Android supports basic bitmap files in a few different formats: 45{@code .png} (preferred), {@code .jpg} (acceptable), {@code .gif} (discouraged).</p> 46 47<p class="note"><strong>Note:</strong> Bitmap files may be automatically optimized with lossless 48image compression by the <a href="{@docRoot}guide/developing/tools/aapt.html">aapt</a> tool. For 49example, a true-color PNG that does not require more than 256 colors may be converted to an 8-bit 50PNG with a color palette. This will result in an image of equal quality but which requires less 51memory. So be aware that the image binaries placed in this directory can change during the build. If 52you plan on reading an image as a bit stream in order to convert it to a bitmap, put your images in 53the <code>res/raw/</code> folder instead, where they will not be optimized.</p> 54 55<dl class="xml"> 56 57<dt>file location:</dt> 58<dd><code>res/drawable/<em>filename</em>.png</code> ({@code .png}, {@code .jpg}, or {@code .gif})<br/> 59The filename will be used as the resource ID.</dd> 60 61<dt>compiled resource datatype:</dt> 62<dd>Resource pointer to a {@link android.graphics.drawable.BitmapDrawable}.</dd> 63 64<dt>resource reference:</dt> 65<dd> 66In Java: <code>R.drawable.<em>filename</em></code></li><br/> 67In XML: <code>@[<em>package</em>:]drawable/<em>filename</em></code> 68</dd> 69 70<dt>example:</dt> 71<dd>With an image saved at <code>res/drawable/myimage.png</code>, this layout XML will apply 72the image to a View: 73<pre> 74<ImageView 75 android:layout_height="wrap_content" 76 android:layout_width="wrap_content" 77 <strong>android:src="@drawable/myimage"</strong> /> 78</pre> 79<p>This application code will retrieve the image as a {@link 80android.graphics.drawable.Drawable}:</p> 81<pre> 82Resources res = {@link android.content.Context#getResources()}; 83Drawable drawable = res.{@link android.content.res.Resources#getDrawable(int) getDrawable}(R.drawable.myimage); 84</pre> 85</dd> 86 87<dt>see also:</dt> 88<dd> 89<ul> 90 <li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html">2D Graphics</a></li> 91 <li>{@link android.graphics.drawable.BitmapDrawable}</li> 92</ul> 93</dd> 94 95</dl> 96 97 98 99 100 101 102 103<h2 id="NinePatch">Nine-Patch File</h2> 104 105<p>A {@link android.graphics.NinePatch} is a PNG image in which you can define stretchable regions 106that Android will scale when content within the View exceeds the normal image bounds. You will 107typically assign this type of image as the background of a View that has at least one dimension set 108to {@code "wrap_content"}, and when the View grows to accomodate the content, the Nine-Patch image 109will also be scaled to match the size of the View. An example use of a Nine-Patch image is the 110background used by Android's standard {@link android.widget.Button} widget, which must stretch to 111accommodate the text (or image) inside the button.</p> 112 113<p>For a complete discussion about how to define a Nine-Patch file with stretchable regions, 114see the <a href="{@docRoot}guide/topics/graphics/2d-graphics.html#nine-patch">2D Graphics</a> 115document.</p> 116 117<dl class="xml"> 118 119<dt>file location:</dt> 120<dd><code>res/drawable/<em>filename</em>.9.png</code><br/> 121The filename will be used as the resource ID.</dd> 122 123<dt>compiled resource datatype:</dt> 124<dd>Resource pointer to a {@link android.graphics.drawable.NinePatchDrawable}.</dd> 125 126<dt>resource reference:</dt> 127<dd> 128In Java: <code>R.drawable.<em>filename</em></code><br/> 129In XML: <code>@[<em>package</em>:]drawable/<em>filename</em></code> 130</dd> 131 132<dt>example:</dt> 133<dd>With an image saved at <code>res/drawable/myninepatch.9.png</code>, this layout XML will 134apply the Nine-Patch to a View: 135<pre> 136<Button 137 android:layout_height="wrap_content" 138 android:layout_width="wrap_content" 139 <strong>android:background="@drawable/myninepatch"</strong> /> 140</pre> 141</dd> 142 143<dt>see also:</dt> 144<dd> 145<ul> 146 <li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html#nine-patch">2D Graphics</a></li> 147 <li>{@link android.graphics.drawable.NinePatchDrawable}</li> 148</ul> 149</dd> 150 151</dl> 152 153 154 155 156 157 158 159 160<h2 id="StateList">State List</h2> 161 162<p>A {@link android.graphics.drawable.StateListDrawable} is a drawable object defined in XML 163that uses a several different images to represent the same graphic, depending on the state of 164the object. For example, a {@link 165android.widget.Button} widget can exist in one of several different states (pressed, focused, 166or niether) and, using a state list drawable, you can provide a different button image for each 167state.</p> 168 169<p>You can describe the state list in an XML file. Each graphic is represented by an {@code 170<item>} element inside a single {@code <selector>} element. Each {@code <item>} 171uses various attributes to describe the state in which it should be used as the graphic for the 172drawable.</p> 173<p>During each state change, the state list is traversed top to bottom and the first item that 174matches the current state will be used—the selection is <em>not</em> based on the "best 175match," but simply the first item that meets the minimum criteria of the state.</p> 176 177<dl class="xml"> 178 179<dt>file location:</dt> 180<dd><code>res/drawable/<em>filename</em>.xml</code><br/> 181The filename will be used as the resource ID.</dd> 182 183<dt>compiled resource datatype:</dt> 184<dd>Resource pointer to a {@link android.graphics.drawable.StateListDrawable}.</dd> 185 186<dt>resource reference:</dt> 187<dd> 188In Java: <code>R.drawable.<em>filename</em></code><br/> 189In XML: <code>@[<em>package</em>:]drawable/<em>filename</em></code> 190</dd> 191 192<dt>syntax:</dt> 193<dd> 194<pre class="stx"> 195<?xml version="1.0" encoding="utf-8"?> 196<<a href="#selector-element">selector</a> xmlns:android="http://schemas.android.com/apk/res/android" 197 android:constantSize=["true" | "false"] 198 android:dither=["true" | "false"] 199 android:variablePadding=["true" | "false"] > 200 <<a href="#item-element">item</a> 201 android:drawable="@[package:]drawable/<em>drawable_resource</em>" 202 android:state_pressed=["true" | "false"] 203 android:state_focused=["true" | "false"] 204 android:state_selected=["true" | "false"] 205 android:state_active=["true" | "false"] 206 android:state_checkable=["true" | "false"] 207 android:state_checked=["true" | "false"] 208 android:state_enabled=["true" | "false"] 209 android:state_window_focused=["true" | "false"] /> 210</selector> 211</pre> 212</dd> 213 214<dt>elements:</dt> 215<dd> 216<dl class="tag-list"> 217 218 <dt id="selector-element"><code><selector></code></dt> 219 <dd><strong>Required.</strong> This must be the root element. Contains one or more {@code 220<item>} elements. 221 <p class="caps">attributes:</p> 222 <dl class="atn-list"> 223 <dt><code>xmlns:android</code></dt> 224 <dd><em>String</em>. <strong>Required.</strong> Defines the XML namespace, which must be 225 <code>"http://schemas.android.com/apk/res/android"</code>. 226 <dt><code>android:constantSize</code></dt> 227 <dd><em>Boolean</em>. "true" if the drawable's reported internal size will remain constant as the state 228changes (the size will be the maximum of all of the states); "false" if the size will vary based on 229the current state. Default is false.</dd> 230 <dt><code>android:dither</code></dt> 231 <dd><em>Boolean</em>. "true" to enable dithering of the bitmap if the bitmap does not have the same pixel 232configuration as the screen (for instance, an ARGB 8888 bitmap with an RGB 565 screen); "false" to 233disable dithering. Default is true.</dd> 234 <dt><code>android:variablePadding</code></dt> 235 <dd><em>Boolean</em>. "true" if the drawable's padding should change based on the current 236state that is selected; "false" if the padding should stay the same (based on the maximum 237padding of all the states). Enabling this feature requires that you deal with 238performing layout when the state changes, which is often not supported. Default is false.</dd> 239 </dl> 240 </dd> 241 <dt id="item-element"><code><item></code></dt> 242 <dd>Defines a drawable to use during certain states, as described by its attributes. Must be a 243child of a <code><selector></code> element. 244 <p class="caps">attributes:</p> 245 <dl class="atn-list"> 246 <dt><code>android:drawable</code></dt> 247 <dd><em>Drawable resource</em>. <strong>Required</strong>. Reference to a drawable resource.</dd> 248 <dt><code>android:state_pressed</code></dt> 249 <dd><em>Boolean</em>. "true" if this item should be used when the object is pressed (such as when a button 250is touched/clicked); "false" if this item should be used in the default, non-pressed state.</dd> 251 <dt><code>android:state_focused</code></dt> 252 <dd><em>Boolean</em>. "true" if this item should be used when the object is focused (such as when a button 253is highlighted using the trackball/d-pad); "false" if this item should be used in the default, 254non-focused state.</dd> 255 <dt><code>android:state_selected</code></dt> 256 <dd><em>Boolean</em>. "true" if this item should be used when the object is selected (such as when a 257tab is opened); "false" if this item should be used when the object is not selected.</dd> 258 <dt><code>android:state_checkable</code></dt> 259 <dd><em>Boolean</em>. "true" if this item should be used when the object is checkable; "false" if this 260item should be used when the object is not checkable. (Only useful if the object can 261transition between a checkable and non-checkable widget.)</dd> 262 <dt><code>android:state_checked</code></dt> 263 <dd><em>Boolean</em>. "true" if this item should be used when the object is checked; "false" if it 264should be used when the object is un-checked.</dd> 265 <dt><code>android:state_enabled</code></dt> 266 <dd><em>Boolean</em>. "true" if this item should be used when the object is enabled (capable of 267receiving touch/click events); "false" if it should be used when the object is disabled.</dd> 268 <dt><code>android:state_window_focused</code></dt> 269 <dd><em>Boolean</em>. "true" if this item should be used when the application window has focus (the 270application is in the foreground), "false" if this item should be used when the application 271window does not have focus (for example, if the notification shade is pulled down or a dialog appears).</dd> 272 </dl> 273 <p class="note"><strong>Note:</strong>Remember that the first item in the state list that 274matches the current state of the object will be applied. So if the first item in the list contains 275none of the state attributes above, then it will be applied every time, which is why your 276default value should always be last (as demonstrated in the following example).</p> 277 </dd> 278 279</dl> 280</dd> <!-- end elements and attributes --> 281 282<dt>example:</dt> 283<dd>XML file saved at <code>res/drawable/button.xml</code>: 284<pre> 285<?xml version="1.0" encoding="utf-8"?> 286<selector xmlns:android="http://schemas.android.com/apk/res/android"> 287 <item android:state_pressed="true" 288 android:drawable="@drawable/button_pressed" /> <!-- pressed --> 289 <item android:state_focused="true" 290 android:drawable="@drawable/button_focused" /> <!-- focused --> 291 <item android:drawable="@drawable/button_normal" /> <!-- default --> 292</selector> 293</pre> 294 295<p>This layout XML will apply the drawable to a View:</p> 296<pre> 297<ImageView 298 android:layout_height="wrap_content" 299 android:layout_width="wrap_content" 300 <strong>android:src="@drawable/button"</strong> /> 301</pre> 302</dd> <!-- end example --> 303 304<dt>see also:</dt> 305<dd> 306<ul> 307 <li>{@link android.graphics.drawable.StateListDrawable}</li> 308</ul> 309</dd> 310 311</dl> 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326<h2 id="Color">Color</h2> 327 328<p>This is a color defined in XML that's used as a drawable to fill a rectangular space, 329with optionally rounded corners. This kind of drawable behaves like a color fill.</p> 330 331<p class="note"><strong>Note:</strong> A color drawable is a simple resource that is referenced 332using the value provided in the {@code name} attribute (not the name of the XML file). As 333such, you can combine a color drawable resources with other simple resources in the one XML file, 334under one {@code <resources>} element.</p> 335 336 337<dl class="xml"> 338 339<dt>file location:</dt> 340<dd><code>res/drawable/<em>filename</em>.png</code><br/> 341The filename is arbitrary. The element's {@code name} will be used as the resource ID.</dd> 342 343<dt>compiled resource datatype:</dt> 344<dd>Resource pointer to a {@link android.graphics.drawable.PaintDrawable}.</dd> 345 346<dt>resource reference:</dt> 347<dd> 348In Java: <code>R.drawable.<em>color_name</em></code><br/> 349In XML: <code>@[<em>package</em>:]drawable/<em>color_name</em></code> 350</dd> 351 352<dt>syntax:</dt> 353<dd> 354<pre class="stx"> 355<?xml version="1.0" encoding="utf-8"?> 356<<a href="#color-resources-element">resources</a>> 357 <<a href="#drawable-element">drawable</a> 358 name="<em>color_name</em>" 359 ><em>color</em></drawable> 360</resources> 361</pre> 362</dd> 363 364<dt>elements:</dt> 365<dd> 366<dl class="tag-list"> 367 368 <dt id="color-resources-element"><code><resources></code></dt> 369 <dd><strong>Required.</strong> This must be the root node. 370 <p>No attributes.</p> 371 </dd> 372 <dt id="drawable-element"><code><drawable></code></dt> 373 <dd>A color to use as a drawable rectangle. The value can be 374 any valid hexadecimal color value or a <a href="more-resources.html#Color">color 375 resource</a>. A color value always begins with a pound (#) character, followed 376 by the Alpha-Red-Green-Blue information in one of the following formats: 377 #<em>RGB</em>, #<em>RRGGBB</em>, #<em>ARGB</em>, or #<em>AARRGGBB</em>. 378 <p class="caps">attributes:</p> 379 <dl class="atn-list"> 380 <dt><code>name</code></dt> 381 <dd><em>String</em>. <strong>Required</strong>. 382 A name for the color. This name will be used as the resource ID.</dd> 383 </dl> 384 385 </dd> 386 387</dl> 388</dd> <!-- end elements and attributes --> 389 390<dt>example:</dt> 391<dd>XML file saved at <code>res/drawable/colors.xml</code>: 392<pre> 393<?xml version="1.0" encoding="utf-8"?> 394<resources> 395 <drawable name="solid_red">#f00</drawable> 396 <drawable name="solid_blue">#0000ff</drawable> 397</resources> 398</pre> 399 <p>This layout XML will apply a color drawable to a View:</p> 400<pre> 401<TextView 402 android:layout_width="fill_parent" 403 android:layout_height="wrap_content" 404 <strong>android:background="@drawable/solid_blue"</strong> /> 405</pre> 406 <p>This application code will get a color drawable and apply it to a View:</p> 407<pre> 408Resources res = {@link android.content.Context#getResources()}; 409Drawable redDrawable = res.{@link android.content.res.Resources#getDrawable(int) getDrawable}(R.drawable.solid_red); 410 411TextView tv = (TextView) findViewByID(R.id.text); 412tv.setBackground(redDrawable); 413</pre> 414</dd> <!-- end example --> 415 416<dt>see also:</dt> 417<dd> 418<ul> 419 <li>{@link android.graphics.drawable.PaintDrawable}</li> 420</ul> 421</dd> 422 423</dl> 424 425 426 427 428 429 430 431 432 433 434 435 436<h2 id="Shape">Shape</h2> 437 438<p>This is a generic shape defined in XML.</p> 439 440<dl class="xml"> 441 442<dt>file location:</dt> 443<dd><code>res/drawable/<em>filename</em>.xml</code><br/> 444The filename will be used as the resource ID.</dd> 445 446<dt>compiled resource datatype:</dt> 447<dd>Resource pointer to a {@link android.graphics.drawable.ShapeDrawable}.</dd> 448 449<dt>resource reference:</dt> 450<dd> 451In Java: <code>R.drawable.<em>filename</em></code><br/> 452In XML: <code>@[<em>package</em>:]drawable/<em>filename</em></code> 453</dd> 454 455<dt>syntax:</dt> 456<dd> 457<pre class="stx"> 458<?xml version="1.0" encoding="utf-8"?> 459<<a href="#shape-element">shape</a> xmlns:android="http://schemas.android.com/apk/res/android" 460 android:shape=["rectangle" | "oval" | "line" | "ring"] > 461 <<a href="#gradient-element">gradient</a> 462 android:angle="<em>integer</em>" 463 android:centerX="<em>integer</em>" 464 android:centerY="<em>integer</em>" 465 android:centerColor="<em>integer</em>" 466 android:endColor="<em>color</em>" 467 android:gradientRadius="<em>integer</em>" 468 android:startColor="<em>color</em>" 469 android:type=["linear" | "radial" | "sweep"] 470 android:usesLevel=["true" | "false"] /> 471 <<a href="#solid-element">solid</a> 472 android:color="<em>color</em>" /> 473 <<a href="#stroke-element">stroke</a> 474 android:width="<em>integer</em>" 475 android:color="<em>color</em>" 476 android:dashWidth="<em>integer</em>" 477 android:dashGap="<em>integer</em>" /> 478 <<a href="#padding-element">padding</a> 479 android:left="<em>integer</em>" 480 android:top="<em>integer</em>" 481 android:right="<em>integer</em>" 482 android:bottom="<em>integer</em>" /> 483 <<a href="#corners-element">corners</a> 484 android:radius="<em>integer</em>" 485 android:topLeftRadius="<em>integer</em>" 486 android:topRightRadius="<em>integer</em>" 487 android:bottomLeftRadius="<em>integer</em>" 488 android:bottomRightRadius="<em>integer</em>" /> 489</shape> 490</pre> 491</dd> 492 493<dt>elements:</dt> 494<dd> 495<dl class="tag-list"> 496 497 <dt id="shape-element"><code><shape></code></dt> 498 <dd><strong>Required.</strong> This must be the root element. 499 <p class="caps">attributes:</p> 500 <dl class="atn-list"> 501 <dt><code>android:shape</code></dt> 502 <dd><em>Keyword</em>. Defines the type of shape. Valid values are: 503 <table> 504 <tr><th>Value</th><th>Desciption</th></tr> 505 <tr><td>{@code "rectangle"}</td> 506 <td>A rectangle that fills the containing View. This is the default shape.</td></tr> 507 <tr><td>{@code "oval"}</td> 508 <td>An oval shape that fits the dimensions of the containing View.</td></tr> 509 <tr><td>{@code "line"}</td> 510 <td>A horizontal line that spans the width of the containing View. This 511 shape requires the {@code <stroke>} element to define the width of the 512 line.</td></tr> 513 <tr><td>{@code "ring"}</td> 514 <td>A ring shape.</td></tr> 515 </table> 516 </dd> 517 </dl> 518 <p>The following attributes are used only when {@code android:shape="ring"}:</p> 519 <dl class="atn-list"> 520 <dt><code>android:innerRadius</code></dt> 521 <dd><em>Dimension</em>. The radius for the 522inner part of the ring (the hole in the middle), as a dimension value or <a 523href="more-resources.html#Dimension">dimension resource</a>.</dd> 524 <dt><code>android:innerRadiusRatio</code></dt> 525 <dd><em>Float</em>. The radius for the inner 526part of the ring, expressed as a ratio of the ring's width. For instance, if {@code 527android:innerRadiusRatio="5"}, then the inner radius equals the ring's width divided by 5. This 528value will be overridden by {@code android:innerRadius}. Default value is 9.</dd> 529 <dt><code>android:thickness</code></dt> 530 <dd><em>Dimension</em>. The thickness of the 531ring, as a dimension value or <a 532href="more-resources.html#Dimension">dimension resource</a>.</dd> 533 <dt><code>android:thicknessRatio</code></dt> 534 <dd><em>Float</em>. The thickness of the ring, 535expressed as a ratio of the ring's width. For instance, if {@code android:thicknessRatio="2"}, then 536the thickness equals the ring's width divided by 2. This value will be overridden by {@code 537android:innerRadius}. Default value is 3.</dd> 538 <dt><code>android:useLevel</code></dt> 539 <dd><em>Boolean</em>. "true" if this is used as 540a {@link android.graphics.drawable.LevelListDrawable}. This should normally be "false" 541 or your shape may not appear.</dd> 542 </dl> 543 <dt id="gradient-element"><code><gradient></code></dt> 544 <dd>Specifies a gradient color for the shape. 545 <p class="caps">attributes:</p> 546 <dl class="atn-list"> 547 <dt><code>android:angle</code></dt> 548 <dd><em>Integer</em>. The angle for the gradient, in degrees. 0 is left to right, 90 is 549bottom to top. It must be a multiple of 45. Default is 0.</dd> 550 <dt><code>android:centerX</code></dt> 551 <dd><em>Float</em>. The relative X-position for the center of the gradient (0 - 1.0). 552Does not apply when {@code android:type="linear"}.</dd> 553 <dt><code>android:centerY</code></dt> 554 <dd><em>Float</em>. The relative Y-position for the center of the gradient (0 - 1.0). 555Does not apply when {@code android:type="linear"}.</dd> 556 <dt><code>android:centerColor</code></dt> 557 <dd><em>Color</em>. Optional color that comes between the start and end colors, as a 558hexadecimal value or <a href="more-resources.html#Color">color resource</a>.</dd> 559 <dt><code>android:endColor</code></dt> 560 <dd><em>Color</em>. The ending color, as a hexadecimal 561value or <a href="more-resources.html#Color">color resource</a>.</dd> 562 <dt><code>android:gradientRadius</code></dt> 563 <dd><em>Float</em>. The radius for the gradient. Only applied when {@code 564android:type="radial"}.</dd> 565 <dt><code>android:startColor</code></dt> 566 <dd><em>Color</em>. The starting color, as a hexadecimal 567value or <a href="more-resources.html#Color">color resource</a>.</dd> 568 <dt><code>android:type</code></dt> 569 <dd><em>Keyword</em>. The type of gradient pattern to apply. Valid values are: 570 <table> 571 <tr><th>Value</th><th>Description</th></tr> 572 <tr><td>{@code "linear"}</td> 573 <td>A linear gradient. This is the default.</td></tr> 574 <tr><td>{@code "radial"}</td> 575 <td>A radial gradient. The start color is the center color.</td></tr> 576 <tr><td>{@code "sweep"}</td> 577 <td>A sweeping line gradient. </td></tr> 578 </table> 579 </dd> 580 <dt><code>android:useLevel</code></dt> 581 <dd><em>Boolean</em>. "true" if this is used as a {@link 582android.graphics.drawable.LevelListDrawable}.</dd> 583 </dl> 584 </dd> 585 <dt id="solid-element"><code><solid></code></dt> 586 <dd>A solid color to fill the shape. 587 <p class="caps">attributes:</p> 588 <dl class="atn-list"> 589 <dt><code>android:color</code></dt> 590 <dd><em>Color</em>. The color to apply to the shape, as a hexadecimal 591value or <a href="more-resources.html#Color">color resource</a>.</dd> 592 </dl> 593 </dd> 594 <dt id="stroke-element"><code><stroke></code></dt> 595 <dd>A stroke line for the shape. 596 <p class="caps">attributes:</p> 597 <dl class="atn-list"> 598 <dt><code>android:width</code></dt> 599 <dd><em>Dimension</em>. The thickness of the line, as a dimension value or <a 600href="more-resources.html#Dimension">dimension resource</a>.</dd> 601 <dt><code>android:color</code></dt> 602 <dd><em>Color</em>. The color of the line, as a 603hexadecimal value or <a href="more-resources.html#Color">color resource</a>.</dd> 604 <dt><code>android:dashGap</code></dt> 605 <dd><em>Dimension</em>. The distance between line dashes, as a dimension value or <a 606href="more-resources.html#Dimension">dimension resource</a>. Only valid if {@code 607android:dashWidth} is set.</dd> 608 <dt><code>android:dashWidth</code></dt> 609 <dd><em>Dimension</em>. The size of each dash line, as a dimension value or <a 610href="more-resources.html#Dimension">dimension resource</a>. Only valid if {@code 611android:dashGap} is set.</dd> 612 </dl> 613 </dd> 614 <dt id="padding-element"><code><padding></code></dt> 615 <dd>Padding to apply to the containing View element (this pads the position of the View 616content, not the shape). 617 <p class="caps">attributes:</p> 618 <dl class="atn-list"> 619 <dt><code>android:left</code></dt> 620 <dd><em>Dimension</em>. Left padding, as a dimension value or <a 621href="more-resources.html#Dimension">dimension resource</a>.</dd> 622 <dt><code>android:top</code></dt> 623 <dd><em>Dimension</em>. Top padding, as a dimension value or <a 624href="more-resources.html#Dimension">dimension resource</a>.</dd> 625 <dt><code>android:right</code></dt> 626 <dd><em>Dimension</em>. Right padding, as a dimension value or <a 627href="more-resources.html#Dimension">dimension resource</a>.</dd> 628 <dt><code>android:bottom</code></dt> 629 <dd><em>Dimension</em>. Bottom padding, as a dimension value or <a 630href="more-resources.html#Dimension">dimension resource</a>.</dd> 631 </dl> 632 </dd> 633 <dt id="corners-element"><code><corners></code></dt> 634 <dd>Creates rounded corners for the shape. Applies only when the shape is a rectangle. 635 <p class="caps">attributes:</p> 636 <dl class="atn-list"> 637 <dt><code>android:radius</code></dt> 638 <dd><em>Dimension</em>. The radius for all corners, as a dimension value or <a 639href="more-resources.html#Dimension">dimension resource</a>. This will be overridden for each 640corner by the following attributes.</dd> 641 <dt><code>android:topLeftRadius</code></dt> 642 <dd><em>Dimension</em>. The radius for the top-left corner, as a dimension value or <a 643href="more-resources.html#Dimension">dimension resource</a>.</dd> 644 <dt><code>android:topRightRadius</code></dt> 645 <dd><em>Dimension</em>. The radius for the top-right corner, as a dimension value or <a 646href="more-resources.html#Dimension">dimension resource</a>.</dd> 647 <dt><code>android:bottomLeftRadius</code></dt> 648 <dd><em>Dimension</em>. The radius for the bottom-left corner, as a dimension value or <a 649href="more-resources.html#Dimension">dimension resource</a>.</dd> 650 <dt><code>android:bottomRightRadius</code></dt> 651 <dd><em>Dimension</em>. The radius for the bottom-right corner, as a dimension value or <a 652href="more-resources.html#Dimension">dimension resource</a>.</dd> 653 </dl> 654 <p class="note"><strong>Note:</strong> Every corner must (initially) be provided a corner 655radius greater than zero, or else no corners will be rounded. If you want specific corners 656to <em>not</em> be rounded, a work-around is to use {@code android:radius} to set a default corner 657radius greater than zero, but then override each and every corner with the values you really 658want, providing zero ("0dp") where you don't want rounded corners.</p> 659 </dd> 660 661</dl> 662</dd> <!-- end elements and attributes --> 663 664<dt>example:</dt> 665<dd>XML file saved at <code>res/drawable/gradient_box.xml</code>: 666<pre> 667<?xml version="1.0" encoding="utf-8"?> 668<shape xmlns:android="http://schemas.android.com/apk/res/android" 669 android:shape="rectangle"> 670 <gradient 671 android:startColor="#FFFF0000" 672 android:endColor="#80FF00FF" 673 android:angle="45"/> 674 <padding android:left="7dp" 675 android:top="7dp" 676 android:right="7dp" 677 android:bottom="7dp" /> 678 <corners android:radius="8dp" /> 679</shape> 680</pre> 681 <p>This layout XML will apply the shape drawable to a View:</p> 682<pre> 683<TextView 684 android:background="@drawable/gradient_box" 685 android:layout_height="wrap_content" 686 android:layout_width="wrap_content" /> 687</pre> 688 <p>This application code will get the shape drawable and apply it to a View:</p> 689<pre> 690Resources res = {@link android.content.Context#getResources()}; 691Drawable shape = res. {@link android.content.res.Resources#getDrawable(int) getDrawable}(R.drawable.gradient_box); 692 693TextView tv = (TextView)findViewByID(R.id.textview); 694tv.setBackground(shape); 695</pre> 696</dd> <!-- end example --> 697 698</dl> 699 700 701 702 703 704 705 706 707 708 709 710 711 712