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 10on the device. It includes three main categories of APIs: 11the {@link android.content.res.Resources Resources} for 12retrieving resource data associated with an application; 13{@link android.content.ContentProvider Content Providers} and 14{@link android.content.ContentResolver ContentResolver} for managing and 15publishing persistent data associated with an application; and 16the {@link android.content.pm.PackageManager Package Manager} 17for finding out information about the application packages installed 18on the device.</p> 19 20<p>In addition, the {@link android.content.Context Context} abstract class 21is a base API for pulling these pieces together, allowing you to access 22an 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 63binary at build time for you by the build system. To use a resource, 64you must install it correctly in the source tree and build your 65application. As part of the build process, Java symbols for each 66of the resources are generated that you can use in your source 67code -- this allows the compiler to verify that your application code matches 68up with the resources you defined.</p> 69 70<p>The rest of this section is organized as a tutorial on how to 71use 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 77form the final complete resource functionality. To help understand the 78overall system, here are some brief definitions of the core concepts and 79components you will encounter in using it:</p> 80 81<p><b>Asset</b>: A single blob of data associated with an application. This 82includes Java object files, graphics (such as PNG images), XML files, etc. 83These files are organized in a directory hierarchy that, during final packaging 84of 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 87assets. In addition to collecting raw assets together, it also parses 88resource definitions into binary asset data.</p> 89 90<p><b>Resource Table</b>: A special asset that aapt generates for you, 91describing all of the resources contained in an application/package. 92This file is accessed for you by the Resources class; it is not touched 93directly by applications.</p> 94 95<p><b>Resource</b>: An entry in the Resource Table describing a single 96named value. Broadly, there are two types of resources: primitives and 97bags.</p> 98 99<p><b>Resource Identifier</b>: In the Resource Table all resources are 100identified by a unique integer number. In source code (resource descriptions, 101XML files, Java code) you can use symbolic names that stand as constants for 102the actual resource identifier integer.</p> 103 104<p><b>Primitive Resource</b>: All primitive resources can be written as a 105simple string, using formatting to describe a variety of primitive types 106included in the resource system: integers, colors, strings, references to 107other resources, etc. Complex resources, such as bitmaps and XML 108describes, are stored as a primitive string resource whose value is the path 109of 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 112simple string, holds an arbitrary list of name/value pairs. Each name is 113itself a resource identifier, and each value can hold 114the same kinds of string formatted data as a normal resource. Bags also 115support inheritance: a bag can inherit the values from another bag, selectively 116replacing 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 119for various purposes. For example, drawable resources are used to 120instantiate Drawable objects, so their data is a primitive resource containing 121either a color constant or string path to a bitmap or XML asset. Other 122common resource kinds are string (localized string primitives), color 123(color primitives), layout (a string path to an XML asset describing a view 124layout), and style (a bag resource describing user interface attributes). 125There is also a standard "attr" resource kind, which defines the resource 126identifiers 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 129to supply a set of user interface attributes. For example, a TextView class may 130be given a style resource that defines its text size, color, and alignment. 131In a layout XML file, you associate a style with a bag using the "style" 132attribute, whose value is the name of the style resource.</p> 133 134<p><b>Style Class</b>: Specifies a related set of attribute resources. 135This data is not placed in the resource table itself, but used to generate 136Java constants that make it easier for you to retrieve values out of 137a style resource and/or XML tag's attributes. For example, the 138Android platform defines a "View" style class that 139contains all of the standard view attributes: padding, visibility, 140background, etc.; when View is inflated it uses this style class to 141retrieve those values from the XML file (at which point style and theme 142information 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 145multiple different available values depending on the current configuration. 146The configuration includes the locale (language and country), screen 147orientation, screen density, etc. The current configuration is used to 148select which resource values are in effect when the resource table is 149loaded.</p> 150 151<p><b>Theme</b>: A standard style resource that supplies global 152attribute values for a particular context. For example, when writing a 153Activity the application developer can select a standard theme to use, such 154as the Theme.White or Theme.Black styles; this style supplies information 155such as the screen background image/color, default text color, button style, 156text editor style, text size, etc. When inflating a layout resource, most 157values for widgets (the text color, selector, background) if not explicitly 158set will come from the current theme; style and attribute 159values supplied in the layout can also assign their value from explicitly 160named 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, 163but instead replaces the values of resources that are in another resource table. 164Like a configuration, this is applied at load time 165to the resource data; it can add new configuration values (for example 166strings in a new locale), replace existing values (for example change 167the standard white background image to a "Hello Kitty" background image), 168and modify resource bags (for example change the font size of the Theme.White 169style to have an 18 pt font size). This is the facility that allows the 170user to select between different global appearances of their device, or 171download 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. 185All such resources are defined under the class "android.R". For example, 186you can display the standard application icon in a screen with the following 187code:</p> 188 189<pre class="prettyprint"> 190public 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"> 207public 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. 222The 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 224the 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 238in the <code>values</code> directory:</p> 239 240<pre> 241MyApp/res/values/strings.xml 242</pre> 243 244<p>The strings can now be retrieved by your application through the 245symbol specified in the "id" attribute:</p> 246 247<pre class="prettyprint"> 248public 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 261here comes from our own application's package, not android.R.</p> 262 263<p>Note that the "mainLabel" string is complex, including style information. 264To 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 267must call <code>toString()</code> on the returned resource if it wants 268a 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, 274but with the <color> resource tag. The data for these resources 275must 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 277for 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 290as the previously shown string data, usually you will place the colors in 291their own file:</p> 292 293<pre> 294MyApp/res/values/colors.xml 295</pre> 296 297<p>The colors can now be retrieved by your application through the 298symbol specified in the "id" attribute:</p> 299 300<pre class="prettyprint"> 301public 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 316image in a special resource sub-directory called "drawable". Files here 317are 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 320for selectors, gradients, etc.</p> 321 322<p>The drawable files will be scanned by the 323resource tool, automatically generating a resource entry for each found. 324For example the file <code>res/drawable/<myimage>.<ext></code> 325will result in a resource symbol named "myimage" (without the extension). Note 326that these file names <em>must</em> be valid Java identifiers, and should 327have only lower-case letters.</p> 328 329<p>For example, to use your own custom image as a badge in a screen, 330you can place the image here:</p> 331 332<pre> 333MyApp/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"> 339public 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 353in a resource file very much like colors shown previously. The only 354difference is that here we use the <drawable> tag to create a 355drawable 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 368as color definitions:</p> 369 370<pre> 371MyApp/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 378generated at runtime. These resources are XML files placed in the 379resource directory "layout", and are how you should create the content 380views inside of your screen (instead of creating them by hand) so that 381they can be themed, styled, configured, and overlayed.</p> 382 383<p>Here is a simple layout resource consisting of a single view, a text 384editor:</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> 398MyApp/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"> 404public 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 415to TextView (including textSize, textColor, and textStyle) that we did 416not define in the previous example; in such a sitation, the default values for 417those attributes come from the theme. If we want to customize them, we 418can 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 431usually make sense to vary with theme or overlay) should be defined through 432the 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 438a resource. This is often used in layout files to supply strings (so they 439can be localized) and images (which exist in another file), though a reference 440can 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 443write a layout file that sets the text color size to be the value contained in 444one 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 457text following that is the name of a resource in the form 458of <code>@[package:]type/name</code>. In this case we didn't need to specify 459the package because we are referencing a resource in our own package. To 460reference 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 473strings 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. 486For example, we can create new drawable resources that are aliases for 487existing 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 500attribute in the current theme. This attribute reference can <em>only</em> 501be used in style resources and XML attributes; it allows you to customize the 502look of UI elements by changing them to standard variations supplied by the 503current 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 506one 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 519an '?' prefix instead of '@'. When you use this markup, you are supplying 520the name of an attribute resource that will be looked up in the theme -- 521because the resource tool knows that an attribute resource is expected, 522you 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 526theme 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 533of related attributes. There are two main uses for these resources: 534defining overall visual themes, and describing a set of visual attributes 535to apply to a class in a layout resource. In this section we will look 536at their use to describe themes; later we will look at using them in 537conjunction with layouts.</p> 538 539<p>Like strings, styles are defined through a resource XML file. In the 540situation where we want to define a new theme, we can create a custom theme 541style 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 555called "styles.xml" , and must be placed in the <code>values</code> 556directory:</p> 557 558<pre> 559MyApp/res/values/styles.xml 560</pre> 561 562<p>Similar to how we previously used a system style for an Activity theme, 563you can apply this style to your Activity:</p> 564 565<pre class="prettyprint"> 566public 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> 578attribute to specify another style resource from which it inherits 579its 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 588to tell the compiler the namespace to look in for the resource -- 589the resources you are specifying here are in your application's namespace, 590not the system. This explicit namespace specification ensures that names 591the application uses will not accidentally conflict with those defined by 592the system.</p> 593 594<p>If you don't specify an explicit parent style, it will be inferred 595from the style name -- everything before the final '.' in the name of the 596style being defined is taken as the parent style name. Thus, to make 597another style in your application that inherits from this base Theme style, 598you 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 611can be used in Java just like we did with <code>R.style.Theme</code> 612above.</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 618set of attributes, or want to allow resource overlays to modify the values of 619attributes. Style resources can be used for both of these purposes, to put 620attribute definitions in a single place that can be references by multiple 621XML tags and modified by overlays. To do this, you simply define a 622new 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