1 <html> 2 <head> 3 <script type="text/javascript" src="http://www.corp.google.com/style/prettify.js"></script> 4 <script src="http://www.corp.google.com/eng/techpubs/include/navbar.js" type="text/javascript"></script> 5 </head> 6 7 <body> 8 9 <p>Contains classes for accessing and publishing data 10 on the device. It includes three main categories of APIs: 11 the {@link android.content.res.Resources Resources} for 12 retrieving resource data associated with an application; 13 {@link android.content.ContentProvider Content Providers} and 14 {@link android.content.ContentResolver ContentResolver} for managing and 15 publishing persistent data associated with an application; and 16 the {@link android.content.pm.PackageManager Package Manager} 17 for finding out information about the application packages installed 18 on the device.</p> 19 20 <p>In addition, the {@link android.content.Context Context} abstract class 21 is a base API for pulling these pieces together, allowing you to access 22 an application's resources and transfer data between applications.</p> 23 24 <p>This package builds on top of the lower-level Android packages 25 {@link android.database}, {@link android.text}, 26 {@link android.graphics.drawable}, {@link android.graphics}, 27 {@link android.os}, and {@link android.util}.</p> 28 29 <ol> 30 <li> <a href="#Resources">Resources</a> 31 <ol> 32 <li> <a href="#ResourcesTerminology">Terminology</a> 33 <li> <a href="#ResourcesQuickStart">Examples</a> 34 <ol> 35 <li> <a href="#UsingSystemResources">Using System Resources</a> 36 <li> <a href="#StringResources">String Resources</a> 37 <li> <a href="#ColorResources">Color Resources</a> 38 <li> <a href="#DrawableResources">Drawable Resources</a> 39 <li> <a href="#LayoutResources">Layout Resources</a> 40 <li> <a href="#ReferencesToResources">References to Resources</a> 41 <li> <a href="#ReferencesToThemeAttributes">References to Theme Attributes</a> 42 <li> <a href="#StyleResources">Style Resources</a> 43 <li> <a href="#StylesInLayoutResources">Styles in Layout Resources</a> 44 </ol> 45 </ol> 46 </ol> 47 48 <a name="Resources"></a> 49 <h2>Resources</h2> 50 51 <p>This topic includes a terminology list associated with resources, and a series 52 of examples of using resources in code. For a complete guide on creating and 53 using resources, see the document on <a href="{@docRoot}guide/topics/resources/resources-i18n.html">Resources 54 and Internationalization</a>. For a reference on the supported Android resource types, 55 see <a href="{@docRoot}guide/topics/resources/available-resources.html">Available Resource Types</a>.</p> 56 <p>The Android resource system keeps track of all non-code 57 assets associated with an application. You use the 58 {@link android.content.res.Resources Resources} class to access your 59 application's resources; the Resources instance associated with your 60 application can generally be found through 61 {@link android.content.Context#getResources Context.getResources()}.</p> 62 <p>An application's resources are compiled into the application 63 binary at build time for you by the build system. To use a resource, 64 you must install it correctly in the source tree and build your 65 application. As part of the build process, Java symbols for each 66 of the resources are generated that you can use in your source 67 code -- this allows the compiler to verify that your application code matches 68 up with the resources you defined.</p> 69 70 <p>The rest of this section is organized as a tutorial on how to 71 use resources in an application.</p> 72 73 <a name="ResourcesTerminology"></a> 74 <h3>Terminology</h3> 75 76 <p>The resource system brings a number of different pieces together to 77 form the final complete resource functionality. To help understand the 78 overall system, here are some brief definitions of the core concepts and 79 components you will encounter in using it:</p> 80 81 <p><b>Asset</b>: A single blob of data associated with an application. This 82 includes Java object files, graphics (such as PNG images), XML files, etc. 83 These files are organized in a directory hierarchy that, during final packaging 84 of the application, is bundled together into a single ZIP file.</p> 85 86 <p><b>aapt</b>: The tool that generates the final ZIP file of application 87 assets. In addition to collecting raw assets together, it also parses 88 resource definitions into binary asset data.</p> 89 90 <p><b>Resource Table</b>: A special asset that aapt generates for you, 91 describing all of the resources contained in an application/package. 92 This file is accessed for you by the Resources class; it is not touched 93 directly by applications.</p> 94 95 <p><b>Resource</b>: An entry in the Resource Table describing a single 96 named value. Broadly, there are two types of resources: primitives and 97 bags.</p> 98 99 <p><b>Resource Identifier</b>: In the Resource Table all resources are 100 identified by a unique integer number. In source code (resource descriptions, 101 XML files, Java code) you can use symbolic names that stand as constants for 102 the actual resource identifier integer.</p> 103 104 <p><b>Primitive Resource</b>: All primitive resources can be written as a 105 simple string, using formatting to describe a variety of primitive types 106 included in the resource system: integers, colors, strings, references to 107 other resources, etc. Complex resources, such as bitmaps and XML 108 describes, are stored as a primitive string resource whose value is the path 109 of the underlying Asset holding its actual data.</p> 110 111 <p><b>Bag Resource</b>: A special kind of resource entry that, instead of a 112 simple string, holds an arbitrary list of name/value pairs. Each name is 113 itself a resource identifier, and each value can hold 114 the same kinds of string formatted data as a normal resource. Bags also 115 support inheritance: a bag can inherit the values from another bag, selectively 116 replacing or extending them to generate its own contents.</p> 117 118 <p><b>Kind</b>: The resource kind is a way to organize resource identifiers 119 for various purposes. For example, drawable resources are used to 120 instantiate Drawable objects, so their data is a primitive resource containing 121 either a color constant or string path to a bitmap or XML asset. Other 122 common resource kinds are string (localized string primitives), color 123 (color primitives), layout (a string path to an XML asset describing a view 124 layout), and style (a bag resource describing user interface attributes). 125 There is also a standard "attr" resource kind, which defines the resource 126 identifiers to be used for naming bag items and XML attributes</p> 127 128 <p><b>Style</b>: The name of the resource kind containing bags that are used 129 to supply a set of user interface attributes. For example, a TextView class may 130 be given a style resource that defines its text size, color, and alignment. 131 In a layout XML file, you associate a style with a bag using the "style" 132 attribute, whose value is the name of the style resource.</p> 133 134 <p><b>Style Class</b>: Specifies a related set of attribute resources. 135 This data is not placed in the resource table itself, but used to generate 136 Java constants that make it easier for you to retrieve values out of 137 a style resource and/or XML tag's attributes. For example, the 138 Android platform defines a "View" style class that 139 contains all of the standard view attributes: padding, visibility, 140 background, etc.; when View is inflated it uses this style class to 141 retrieve those values from the XML file (at which point style and theme 142 information is applied as approriate) and load them into its instance.</p> 143 144 <p><b>Configuration</b>: For any particular resource identifier, there may be 145 multiple different available values depending on the current configuration. 146 The configuration includes the locale (language and country), screen 147 orientation, screen density, etc. The current configuration is used to 148 select which resource values are in effect when the resource table is 149 loaded.</p> 150 151 <p><b>Theme</b>: A standard style resource that supplies global 152 attribute values for a particular context. For example, when writing a 153 Activity the application developer can select a standard theme to use, such 154 as the Theme.White or Theme.Black styles; this style supplies information 155 such as the screen background image/color, default text color, button style, 156 text editor style, text size, etc. When inflating a layout resource, most 157 values for widgets (the text color, selector, background) if not explicitly 158 set will come from the current theme; style and attribute 159 values supplied in the layout can also assign their value from explicitly 160 named values in the theme attributes if desired.</p> 161 162 <p><b>Overlay</b>: A resource table that does not define a new set of resources, 163 but instead replaces the values of resources that are in another resource table. 164 Like a configuration, this is applied at load time 165 to the resource data; it can add new configuration values (for example 166 strings in a new locale), replace existing values (for example change 167 the standard white background image to a "Hello Kitty" background image), 168 and modify resource bags (for example change the font size of the Theme.White 169 style to have an 18 pt font size). This is the facility that allows the 170 user to select between different global appearances of their device, or 171 download files with new appearances.</p> 172 173 <a name="ResourcesQuickStart"></a> 174 <h3>Examples</h3> 175 176 <p>This section gives a few quick examples you can use to make your own resources. 177 For more details on how to define and use resources, see <a 178 href="{@docRoot}guide/topics/resources/resources-i18n.html">Resources and 179 Internationalization</a>. </p> 180 181 <a name="UsingSystemResources"></a> 182 <h4>Using System Resources</h4> 183 184 <p>Many resources included with the system are available to applications. 185 All such resources are defined under the class "android.R". For example, 186 you can display the standard application icon in a screen with the following 187 code:</p> 188 189 <pre class="prettyprint"> 190 public class MyActivity extends Activity 191 { 192 public void onStart() 193 { 194 requestScreenFeatures(FEATURE_BADGE_IMAGE); 195 196 super.onStart(); 197 198 setBadgeResource(android.R.drawable.sym_def_app_icon); 199 } 200 } 201 </pre> 202 203 <p>In a similar way, this code will apply to your screen the standard 204 "green background" visual treatment defined by the system:</p> 205 206 <pre class="prettyprint"> 207 public class MyActivity extends Activity 208 { 209 public void onStart() 210 { 211 super.onStart(); 212 213 setTheme(android.R.style.Theme_Black); 214 } 215 } 216 </pre> 217 218 <a name="StringResources"></a> 219 <h4>String Resources</h4> 220 221 <p>String resources are defined using an XML resource description syntax. 222 The file or multiple files containing these resources can be given any name 223 (as long as it has a .xml suffix) and placed at an appropriate location in 224 the source tree for the desired configuration (locale/orientation/density). 225 226 <p>Here is a simple resource file describing a few strings:</p> 227 228 <pre> 229 <?xml version="1.0" encoding="utf-8"?> 230 <resources> 231 <string id="mainLabel">Hello <u>th<ignore>e</ignore>re</u>, <i>you</i> <b>Activity</b>!</string> 232 <string id="back">Back</string> 233 <string id="clear">Clear</string> 234 </resources> 235 </pre> 236 237 <p>Typically this file will be called "strings.xml", and must be placed 238 in the <code>values</code> directory:</p> 239 240 <pre> 241 MyApp/res/values/strings.xml 242 </pre> 243 244 <p>The strings can now be retrieved by your application through the 245 symbol specified in the "id" attribute:</p> 246 247 <pre class="prettyprint"> 248 public class MyActivity extends Activity 249 { 250 public void onStart() 251 { 252 super.onStart(); 253 254 String back = getResources().getString(R.string.back).toString(); 255 back = getString(R.string.back).toString(); // synonym 256 } 257 } 258 </pre> 259 260 <p>Unlike system resources, the resource symbol (the R class) we are using 261 here comes from our own application's package, not android.R.</p> 262 263 <p>Note that the "mainLabel" string is complex, including style information. 264 To support this, the <code>getString()</code> method returns a 265 <code>CharSequence</code> object that you can pass to a 266 <code>TextView</code> to retain those style. This is why code 267 must call <code>toString()</code> on the returned resource if it wants 268 a raw string.</p> 269 270 <a name="ColorResources"></a> 271 <h4>Color Resources</h4> 272 273 <p>Color resources are created in a way very similar to string resources, 274 but with the <color> resource tag. The data for these resources 275 must be a hex color constant of the form "#rgb", "#argb", "#rrggbb", or 276 "#aarrggbb". The alpha channel is 0xff (or 0xf) for opaque and 0 277 for transparent.</p> 278 279 <pre> 280 <?xml version="1.0" encoding="utf-8"?> 281 <resources> 282 <color id="opaque_red">#ffff0000</color> 283 <color id="transparent_red">#80ff0000</color> 284 <color id="opaque_blue">#0000ff</color> 285 <color id="opaque_green">#0f0</color> 286 </resources> 287 </pre> 288 289 <p>While color definitions could be placed in the same resource file 290 as the previously shown string data, usually you will place the colors in 291 their own file:</p> 292 293 <pre> 294 MyApp/res/values/colors.xml 295 </pre> 296 297 <p>The colors can now be retrieved by your application through the 298 symbol specified in the "id" attribute:</p> 299 300 <pre class="prettyprint"> 301 public class MyActivity extends Activity 302 { 303 public void onStart() 304 { 305 super.onStart(); 306 307 int red = getResources().getColor(R.color.opaque_red); 308 } 309 } 310 </pre> 311 312 <a name="DrawableResources"></a> 313 <h4>Drawable Resources</h4> 314 315 <p>For simple drawable resources, all you need to do is place your 316 image in a special resource sub-directory called "drawable". Files here 317 are things that can be handled by an implementation of the 318 {@link android.graphics.drawable.Drawable Drawable} class, often bitmaps 319 (such as PNG images) but also various kinds of XML descriptions 320 for selectors, gradients, etc.</p> 321 322 <p>The drawable files will be scanned by the 323 resource tool, automatically generating a resource entry for each found. 324 For example the file <code>res/drawable/<myimage>.<ext></code> 325 will result in a resource symbol named "myimage" (without the extension). Note 326 that these file names <em>must</em> be valid Java identifiers, and should 327 have only lower-case letters.</p> 328 329 <p>For example, to use your own custom image as a badge in a screen, 330 you can place the image here:</p> 331 332 <pre> 333 MyApp/res/drawable/my_badge.png 334 </pre> 335 336 <p>The image can then be used in your code like this:</p> 337 338 <pre class="prettyprint"> 339 public class MyActivity extends Activity 340 { 341 public void onStart() 342 { 343 requestScreenFeatures(FEATURE_BADGE_IMAGE); 344 345 super.onStart(); 346 347 setBadgeResource(R.drawable.my_badge); 348 } 349 } 350 </pre> 351 352 <p>For drawables that are a single solid color, you can also define them 353 in a resource file very much like colors shown previously. The only 354 difference is that here we use the <drawable> tag to create a 355 drawable resource.</p> 356 357 <pre> 358 <?xml version="1.0" encoding="utf-8"?> 359 <resources> 360 <drawable id="opaque_red">#ffff0000</drawable> 361 <drawable id="transparent_red">#80ff0000</drawable> 362 <drawable id="opaque_blue">#0000ff</drawable> 363 <drawable id="opaque_green">#0f0</drawable> 364 </resources> 365 </pre> 366 367 <p>These resource entries are often placed in the same resource file 368 as color definitions:</p> 369 370 <pre> 371 MyApp/res/values/colors.xml 372 </pre> 373 374 <a name="LayoutResources"></a> 375 <h4>Layout Resources</h4> 376 377 <p>Layout resources describe a view hierarchy configuration that is 378 generated at runtime. These resources are XML files placed in the 379 resource directory "layout", and are how you should create the content 380 views inside of your screen (instead of creating them by hand) so that 381 they can be themed, styled, configured, and overlayed.</p> 382 383 <p>Here is a simple layout resource consisting of a single view, a text 384 editor:</p> 385 386 <pre> 387 <?xml version="1.0" encoding="utf-8"?> 388 <root> 389 <EditText id="text" 390 android:layout_width="fill-parent" android:layout_height="fill-parent" 391 android:text="Hello, World!" /> 392 </root> 393 </pre> 394 395 <p>To use this layout, it can be placed in a file like this:</p> 396 397 <pre> 398 MyApp/res/layout/my_layout.xml 399 </pre> 400 401 <p>The layout can then be instantiated in your screen like this:</p> 402 403 <pre class="prettyprint"> 404 public class MyActivity extends Activity 405 { 406 public void onStart() 407 { 408 super.onStart(); 409 setContentView(R.layout.my_layout); 410 } 411 } 412 </pre> 413 414 <p>Note that there are a number of visual attributes that can be supplied 415 to TextView (including textSize, textColor, and textStyle) that we did 416 not define in the previous example; in such a sitation, the default values for 417 those attributes come from the theme. If we want to customize them, we 418 can supply them explicitly in the XML file:</p> 419 420 <pre> 421 <?xml version="1.0" encoding="utf-8"?> 422 <root> 423 <EditText id="text" 424 android:layout_width="match_parent" android:layout_height="match_parent" 425 <b>android:textSize="18" android:textColor="#008"</b> 426 android:text="Hello, World!" /> 427 </root> 428 </pre> 429 430 <p>However, usually these kinds of attributes (those being attributes that 431 usually make sense to vary with theme or overlay) should be defined through 432 the theme or separate style resource. Later we will see how this is done.</p> 433 434 <a name="ReferencesToResources"></a> 435 <h4>References to Resources</h4> 436 437 <p>A value supplied in an attribute (or resource) can also be a reference to 438 a resource. This is often used in layout files to supply strings (so they 439 can be localized) and images (which exist in another file), though a reference 440 can be do any resource type including colors and integers.</p> 441 442 <p>For example, if we have the previously defined color resources, we can 443 write a layout file that sets the text color size to be the value contained in 444 one of those resources:</p> 445 446 <pre> 447 <?xml version="1.0" encoding="utf-8"?> 448 <root> 449 <EditText id="text" 450 android:layout_width="match_parent" android:layout_height="match_parent" 451 <b>android:textColor="@color/opaque_red"</b> 452 android:text="Hello, World!" /> 453 </root> 454 </pre> 455 456 <p>Note here the use of the '@' prefix to introduce a resource reference -- the 457 text following that is the name of a resource in the form 458 of <code>@[package:]type/name</code>. In this case we didn't need to specify 459 the package because we are referencing a resource in our own package. To 460 reference a system resource, you would need to write:</p> 461 462 <pre> 463 <?xml version="1.0" encoding="utf-8"?> 464 <root> 465 <EditText id="text" 466 android:layout_width="match_parent" android:layout_height="match_parent" 467 android:textColor="@<b>android:</b>color/opaque_red" 468 android:text="Hello, World!" /> 469 </root> 470 </pre> 471 472 <p>As another example, you should always use resource references when supplying 473 strings in a layout file so that they can be localized:</p> 474 475 <pre> 476 <?xml version="1.0" encoding="utf-8"?> 477 <root> 478 <EditText id="text" 479 android:layout_width="match_parent" android:layout_height="match_parent" 480 android:textColor="@android:color/opaque_red" 481 android:text="@string/hello_world" /> 482 </root> 483 </pre> 484 485 <p>This facility can also be used to create references between resources. 486 For example, we can create new drawable resources that are aliases for 487 existing images:</p> 488 489 <pre> 490 <?xml version="1.0" encoding="utf-8"?> 491 <resources> 492 <drawable id="my_background">@android:drawable/theme2_background</drawable> 493 </resources> 494 </pre> 495 496 <a name="ReferencesToThemeAttributes"></a> 497 <h4>References to Theme Attributes</h4> 498 499 <p>Another kind of resource value allows you to reference the value of an 500 attribute in the current theme. This attribute reference can <em>only</em> 501 be used in style resources and XML attributes; it allows you to customize the 502 look of UI elements by changing them to standard variations supplied by the 503 current theme, instead of supplying more concrete values.</p> 504 505 <p>As an example, we can use this in our layout to set the text color to 506 one of the standard colors defined in the base system theme:</p> 507 508 <pre> 509 <?xml version="1.0" encoding="utf-8"?> 510 <root> 511 <EditText id="text" 512 android:layout_width="match_parent" android:layout_height="match_parent" 513 <b>android:textColor="?android:textDisabledColor"</b> 514 android:text="@string/hello_world" /> 515 </root> 516 </pre> 517 518 <p>Note that this is very similar to a resource reference, except we are using 519 an '?' prefix instead of '@'. When you use this markup, you are supplying 520 the name of an attribute resource that will be looked up in the theme -- 521 because the resource tool knows that an attribute resource is expected, 522 you do not need to explicitly state the type (which would be 523 <code>?android:attr/android:textDisabledColor</code>).</p> 524 525 <p>Other than using this resource identifier to find the value in the 526 theme instead of raw resources, the name syntax is identical to the '@' format: 527 <code>?[package:]type/name</code> with the type here being optional.</p> 528 529 <a name="StyleResources"></a> 530 <h4>Style Resources</h4> 531 532 <p>A style resource is a set of name/value pairs describing a group 533 of related attributes. There are two main uses for these resources: 534 defining overall visual themes, and describing a set of visual attributes 535 to apply to a class in a layout resource. In this section we will look 536 at their use to describe themes; later we will look at using them in 537 conjunction with layouts.</p> 538 539 <p>Like strings, styles are defined through a resource XML file. In the 540 situation where we want to define a new theme, we can create a custom theme 541 style that inherits from one of the standard system themes:</p> 542 543 <pre> 544 <?xml version="1.0" encoding="utf-8"?> 545 <resources> 546 <style id="Theme" parent="android:Theme.White"> 547 <item id="android:foregroundColor">#FFF8D96F</item> 548 <item id="android:textColor">@color/opaque_blue</item> 549 <item id="android:textSelectedColor">?android:textColor</item> 550 </style> 551 </resources> 552 </pre> 553 554 <p>Typically these resource definitions will be placed in a file 555 called "styles.xml" , and must be placed in the <code>values</code> 556 directory:</p> 557 558 <pre> 559 MyApp/res/values/styles.xml 560 </pre> 561 562 <p>Similar to how we previously used a system style for an Activity theme, 563 you can apply this style to your Activity:</p> 564 565 <pre class="prettyprint"> 566 public class MyActivity extends Activity 567 { 568 public void onStart() 569 { 570 super.onStart(); 571 572 setTheme(R.style.Theme); 573 } 574 } 575 </pre> 576 577 <p>In the style resource shown here, we used the <code>parent</code> 578 attribute to specify another style resource from which it inherits 579 its values -- in this case the <code>Theme.White</code> system resource:</p> 580 581 <pre> 582 <style id="Home" parent="android:Theme.White"> 583 ... 584 </style> 585 </pre> 586 587 <p>Note, when doing this, that you must use the "android" prefix in front 588 to tell the compiler the namespace to look in for the resource -- 589 the resources you are specifying here are in your application's namespace, 590 not the system. This explicit namespace specification ensures that names 591 the application uses will not accidentally conflict with those defined by 592 the system.</p> 593 594 <p>If you don't specify an explicit parent style, it will be inferred 595 from the style name -- everything before the final '.' in the name of the 596 style being defined is taken as the parent style name. Thus, to make 597 another style in your application that inherits from this base Theme style, 598 you can write:</p> 599 600 <pre> 601 <?xml version="1.0" encoding="utf-8"?> 602 <resources> 603 <style id="Theme.WhiteText"> 604 <item id="android:foregroundColor">#FFFFFFFF</item> 605 <item id="android:textColor">?android:foregroundColor</item> 606 </style> 607 </resources> 608 </pre> 609 610 <p>This results in the symbol <code>R.style.Theme_WhiteText</code> that 611 can be used in Java just like we did with <code>R.style.Theme</code> 612 above.</p> 613 614 <a name="StylesInLayoutResources"></a> 615 <h4>Styles in Layout Resources</h4> 616 617 <p>Often you will have a number fo views in a layout that all use the same 618 set of attributes, or want to allow resource overlays to modify the values of 619 attributes. Style resources can be used for both of these purposes, to put 620 attribute definitions in a single place that can be references by multiple 621 XML tags and modified by overlays. To do this, you simply define a 622 new style resource with the desired values:</p> 623 624 <pre> 625 <?xml version="1.0" encoding="utf-8"?> 626 <resources> 627 <style id="SpecialText"> 628 <item id="android:textSize">18</item> 629 <item id="android:textColor">#008</item> 630 </style> 631 </resources> 632 </pre> 633 634 <p>You can now apply this style to your TextView in the XML file:</p> 635 636 <pre> 637 <?xml version="1.0" encoding="utf-8"?> 638 <root> 639 <EditText id="text1" <b>style="@style/SpecialText"</b> 640 android:layout_width="match_parent" android:layout_height="wrap_content" 641 android:text="Hello, World!" /> 642 <EditText id="text2" <b>style="@style/SpecialText"</b> 643 android:layout_width="match_parent" android:layout_height="wrap_content" 644 android:text="I love you all." /> 645 </root></pre> 646 <h4> </h4> 647 648 </body> 649 </html> 650 651