From c6cb8a78d03cda44a49a990b4d4153560bee7420 Mon Sep 17 00:00:00 2001
From: Scott Main There are two ways you can reference your resources for use in your application: Once you provide a resource in your application (discussed in Providing Resources), you can apply it by
+referencing its resource ID. All resource IDs are defined in your project's {@code R} class, which
+the {@code aapt} tool automatically generates. When your application is compiled, {@code aapt} generates the {@code R} class, which contains
+resource IDs for all the resources in your {@code
+res/} directory. For each type of resource, there is an {@code R} subclass (for example,
+{@code R.drawable} for all drawable resources) and for each resource of that type, there is a static
+integer (for example, {@code R.drawable.icon}). This integer is the resource ID that you can use
+to retrieve your resource. Although the {@code R} class is where resource IDs are specified, you should never need to
+look there to discover a resource ID. A resource ID is always composed of: {@code R.string.hello} You will see Android APIs that accept this kind of resource identifier as a method parameter
-(usually defined as the {@code id} parameter). {@code @string/hello} You can use this syntax in an XML resource any place where a value is expected that is
-matched by the existing resource. For example, if you need to provide a string in either an XML
-attribute or element value, you can reference a resource instead of providing a hard-coded
-string. There are two ways you can access a resource: {@code string} is the resource type and {@code hello} is the resource name. There are many
+Android APIs that can access your resources when you provide a resource ID in this format. See
+Accessing Resources in Code. {@code string} is the resource type and {@code hello} is the resource name. You can use this
+syntax in an XML resource any place where a value is expected that you provide in a resource. See Accessing Resources from XML. When your application is compiled, Android generates the {@code R.java} file (inside
-the {@code gen/} directory), which contains resource
-identifiers to all the resources in your {@code res/} directory. For each type of resource, a
-specific subclass is added to the {@code R} class (for example,
-{@code R.drawable}) and for each resource of that type, a static
-integer is added to the subclass (for example,
-{@code R.drawable.icon}). This integer is the resource ID and you can use it to retrieve
-your resource from your application code. You can use a resource in code by passing the resource ID as a method parameter. For
+example, you can set an {@link android.widget.ImageView} to use the {@code res/drawable/myimage.png}
+resource using {@link android.widget.ImageView#setImageResource(int) setImageResource()}: You can also retrieve individual resources using methods in {@link
+android.content.res.Resources}, which you can get an instance of
+with {@link android.content.Context#getResources()}. While uncommon, you might need access your original files and directories. If you do, then
-saving your files in {@code res/} won't work for you. Instead, you can save your resources in the
+saving your files in {@code res/} won't work for you, because the only way to read a resource from
+{@code res/} is with the resource ID. Instead, you can save your resources in the
{@code assets/} directory. Files saved in the {@code assets/} directory will not be given a resource
+ Files saved in the {@code assets/} directory are not given a resource
ID, so you can't reference them through the {@code R} class or from XML resources. Instead, you can
query files in the {@code assets/} directory like a normal file system and read raw data using
{@link android.content.res.AssetManager}. However, if all you require is the ability to read raw data (such as a video or audio file),
then save the file in the {@code res/raw/} directory and read a stream of bytes using {@link
-android.content.res.Resources#openRawResource(int)}. Caution: You should never modify the {@code
-R.java} file by hand—it is generated by the {@code aapt} tool when your project is
-compiled. Any changes will be overridden next time you compile. Here is the syntax to reference a resource in code:
- Here's the syntax to reference a resource in code: See Resource Types for
more information about each resource type and how to reference them. In many cases, you can supply an API method with the resource ID. For example, to set the image
-for an {@link android.widget.ImageView}: You can also retrieve your
-resource objects using methods in {@link android.content.res.Resources}, which you can create an
-instance of with {@link android.content.Context#getResources Context.getResources()}. For example,
-to get a string: You can also access resources from the platform by prefixing the {@code android}
-namespace. Android contains a number of standard resources, such as styles and themes for your
-layout, button backgrounds, and layouts. To refer to these in code, qualify your reference with the
- There are many methods that accept a resource ID parameter and you can retrieve resources using
+methods in {@link android.content.res.Resources}. You can get an instance of {@link
+android.content.res.Resources} with {@link android.content.Context#getResources
+Context.getResources()}. Here are some examples of using resources in code: Here are some examples of accessing resources in code: Caution: You should never modify the {@code
+R.java} file by hand—it is generated by the {@code aapt} tool when your project is
+compiled. Any changes are overridden next time you compile. You can define values for some XML attributes and elements using a
+reference to an existing resource. You will often do this when creating layout files, to
+supply strings and images for your widgets. When creating an XML resource, some values for attributes and elements can be a reference to
-an existing resource. This is often used in layout files to supply strings and images. For example, if you add a {@link android.widget.Button} to your layout, you should use
+a string resource for the button text: Here is the syntax to reference a resource in an XML resource: See Resource Types for
more information about each resource type and how to reference them. For example, if you have the following resource file that includes a Use cases
+
+ In some cases you must use a resource for a value in XML (for example, to apply a drawable image
+to a widget), but you can also use a resource in XML any place that accepts a simple value. For
+example, if you have the following resource file that includes a color resource and a string resource: In this case you don't need to specify the package name in the resource reference because the
@@ -219,19 +260,18 @@ reference a system resource, you would need to include the package name. For exa
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
- android:textColor="@android:color/secondary_text_dark"
+ android:textColor="@android:color/secondary_text_dark"
android:text="@string/hello" />
- Note: You should always use a string resource when supplying
-strings in a layout file, as demonstrated above, so that the strings can be localized. For
-information about creating alternative resources (such as localized strings), see Note: You should use string resources at all times, so that your
+application can be localized for other languages. For information about creating alternative
+resources (such as localized strings), see Providing Alternative
Resources. This facility for referencing resources between resources can also be used to create
-alias resources. For example, you can create new drawable resources that is an alias for an existing
-image: You can even use resources in XML to create aliases. For example, you can create a
+drawable resource that is an alias for another drawable resource:In this document
-
See also
@@ -39,154 +40,194 @@ parent.link=index.html
-
-
+
+
+
-R.string.hello
+ @string/hello
+ Accessing Resources in Code
+Accessing Resources in Code
-
+ImageView imageView = (ImageView) findViewById(R.id.myimageview);
+imageView.setImageResource(R.drawable.myimage);
+
+Access to Original Files
Syntax
-[<package_name>.]R.<resource_type>.<resource_name>
-
+[<package_name>.]R.<resource_type>.<resource_name>
+
-ImageView iv = (ImageView) findViewById(R.id.myimageview);
-iv.setImageResource(R.drawable.myimage);
-
-
-Resources res = this.getResources();
-String string = res.getString(R.string.mystring);
-
+Use cases
-android package name. For example,
-android.R.layout.simple_gallery_item.
// Load a background for the current screen from a drawable resource
{@link android.app.Activity#getWindow()}.{@link
android.view.Window#setBackgroundDrawableResource(int)
-setBackgroundDrawableResource}(R.drawable.my_background_image) ;
+setBackgroundDrawableResource}(R.drawable.my_background_image) ;
// Set the Activity title by getting a string from the Resources object, because
// this method requires a CharSequence rather than a resource ID
{@link android.app.Activity#getWindow()}.{@link android.view.Window#setTitle(CharSequence)
setTitle}(getResources().{@link android.content.res.Resources#getText(int)
-getText}(R.string.main_title));
+getText}(R.string.main_title));
// Load a custom layout for the current screen
{@link android.app.Activity#setContentView(int)
-setContentView}(R.layout.main_screen);
+setContentView}(R.layout.main_screen);
// Set a slide in animation by getting an Animation from the Resources object
mFlipper.{@link android.widget.ViewAnimator#setInAnimation(Animation)
setInAnimation}(AnimationUtils.loadAnimation(this,
- R.anim.hyperspace_in));
+ R.anim.hyperspace_in));
// Set the text on a TextView object using a resource ID
-TextView msgTextView = (TextView) findViewById(R.id.msg);
-msgTextView.{@link android.widget.TextView#setText(int) setText}(R.string.hello_message);
+TextView msgTextView = (TextView) findViewById(R.id.msg);
+msgTextView.{@link android.widget.TextView#setText(int)
+setText}(R.string.hello_message);
+Accessing Resources from XML
-Accessing Resources in other XML Resources
+
+<Button
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/submit" />
+
+
+
+Syntax
@[<package_name>:]<resource_type>/<resource_name>
+@[<package_name>:]<resource_type>/<resource_name>
+
<?xml version="1.0" encoding="utf-8"?>
@@ -239,15 +279,14 @@ image:
This is discussed further in Creating -alias resources.
- +This sounds redundant, but can be very useful when using alternative resource. Read more about +Creating alias resources.
A style attribute resource is another type of resource that allows you to reference the value +
A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute @@ -257,28 +296,46 @@ essentially says, "use the style that is defined by this attribute, in the curre format, but instead of the at-symbol ({@code @}), use a question-mark ({@code ?}), and the resource type portion is optional. For instance:
-
-
+
-
?[<package_name>:][<resource_type>/]<resource_name>
-
For example, here's how you might reference an attribute in a layout, -to set the text color to match the "primary" text color of the system theme:
+For example, here's how you can reference an attribute to set the text color to match the +"primary" text color of the system theme:
<EditText id="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
- android:textColor="?android:textColorSecondary"
+ android:textColor="?android:textColorSecondary"
android:text="@string/hello_world" />
-Using this markup, you are -supplying the name of an attribute resource that will be looked up in the theme. -Because the system resource tool knows that an attribute resource is expected, +
Here, the {@code android:textColor} attribute specifies the name of a style attribute
+in the current theme. Android now uses the value applied to the {@code android:textColorSecondary}
+style attribute as the value for {@code android:textColor} in this widget. Because the system
+resource tool knows that an attribute resource is expected in this context,
you do not need to explicitly state the type (which would be
-?android:attr/textColorSecondary), so you can exclude the {@code attr} type.
?android:attr/textColorSecondary)—you can exclude the {@code attr} type.
+
+Android contains a number of standard resources, such as styles, themes, and layouts. To
+access these resource, qualify your resource reference with the
+android package name. For example, Android provides a layout resource you can use for
+list items in a {@link android.widget.ListAdapter}:
+{@link android.app.ListActivity#setListAdapter(ListAdapter)
+setListAdapter}(new {@link
+android.widget.ArrayAdapter}<String>(this, android.R.layout.simple_list_item_1, myarray));
+
+
+In this example, {@link android.R.layout#simple_list_item_1} is a layout resource defined by the +platform for items in a {@link android.widget.ListView}. You can use this instead of creating +your own layout for list items. (For more about using {@link android.widget.ListView}, see the +List View Tutorial.)
+ diff --git a/docs/html/guide/topics/resources/animation-resource.jd b/docs/html/guide/topics/resources/animation-resource.jd index b2fab04e4aab0..e0ce0515d6149 100644 --- a/docs/html/guide/topics/resources/animation-resource.jd +++ b/docs/html/guide/topics/resources/animation-resource.jd @@ -62,18 +62,18 @@ In XML:@[package:]anim/filename
android:toXScale="float"
android:fromYScale="float"
android:toYScale="float"
- android:pivotX="string"
- android:pivotY="string" />
+ android:pivotX="float"
+ android:pivotY="float" />
<translate
- android:fromX="string"
- android:toX="string"
- android:fromY="string"
- android:toY="string" />
+ android:fromX="float"
+ android:toX="float"
+ android:fromY="float"
+ android:toY="float" />
<rotate
android:fromDegrees="float"
android:toDegrees="float"
- android:pivotX="string"
- android:pivotY="string" />
+ android:pivotX="float"
+ android:pivotY="float" />
<set>
...
</set>
@@ -158,21 +158,21 @@ android.view.animation.TranslateAnimation}.
attributes:
android:fromXDeltaandroid:toXDeltaandroid:fromYDeltaandroid:toYDeltaFor more attributes supported by <translate>, see the
{@link android.view.animation.Animation} class reference (of which, all XML attributes are
@@ -183,15 +183,19 @@ inherrited by this element).
attributes:
android:fromDegreesandroid:toDegreesandroid:pivotXandroid:pivotYFor more attributes supported by <rotate>, see the
{@link android.view.animation.Animation} class reference (of which, all XML attributes are
diff --git a/docs/html/guide/topics/resources/available-resources.jd b/docs/html/guide/topics/resources/available-resources.jd
index 19babee6c9220..09c55a5a1ab05 100644
--- a/docs/html/guide/topics/resources/available-resources.jd
+++ b/docs/html/guide/topics/resources/available-resources.jd
@@ -18,6 +18,23 @@ of application resource that you can provide in your resources directory ({@code
Here's a brief summary of each resource type:
+You will often use an {@code R.id} integer to handle {@link android.view.View} objects in +your UI. Although the {@code id} is a subclass of the {@code R} class, it is not considered a +"resource" because it is not a reference to an externalized application resource. The {@code id} +is simply a unique identifier that allows you to handle elements in your UI by instantiating +objects with {@link android.app.Activity#findViewById(int) findViewById()}.
+ +For information about using {@code R.id} with your UI, see Declaring Layout.
+ +@[package:]drawable/filename
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
+ android:endColor="color"
android:gradientRadius="integer"
- android:type=""
- android:usesLevel=""
android:startColor="color"
- android:endColor="color" />
+ android:type=["linear" | "radial" | "sweep"]
+ android:usesLevel=["true" | "false"] />
<solid
android:color="color" />
<stroke
@@ -544,18 +544,6 @@ a {@link android.graphics.drawable.LevelListDrawable}. This should normally be "
attributes:
android:type| Value | Description |
|---|---|
| {@code "linear"} | -A linear gradient. This is the default. |
| {@code "radial"} | -A radial gradient. The start color is the center color. |
| {@code "sweep"} | -A sweeping line gradient. |
android:angleandroid:centerColorandroid:gradientRadiusandroid:useLevelandroid:startColorandroid:endColorandroid:gradientRadiusandroid:startColorandroid:type| Value | Description |
|---|---|
| {@code "linear"} | +A linear gradient. This is the default. |
| {@code "radial"} | +A radial gradient. The start color is the center color. |
| {@code "sweep"} | +A sweeping line gradient. |
android:useLevel<solid>
Figure 1. Two device configurations, both using default resources.
Figure 2. Two device configurations, one using alternative
diff --git a/docs/html/guide/topics/resources/layout-resource.jd b/docs/html/guide/topics/resources/layout-resource.jd
index a6b177f01fc6b..2c51d54f41e0b 100644
--- a/docs/html/guide/topics/resources/layout-resource.jd
+++ b/docs/html/guide/topics/resources/layout-resource.jd
@@ -36,14 +36,14 @@ In XML: @[package:]layout/filename
<?xml version="1.0" encoding="utf-8"?>
<ViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/name"
- android:layout_height="@+id/string_name"
- android:layout_width="@+id/string_name"
- [other attributes] >
+ android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
+ android:layout_width=["dimension" | "fill_parent" | "wrap_content"]
+ [ViewGroup-specific attributes] >
<View
android:id="@+id/name"
- android:layout_height="@+id/string_name"
- android:layout_width="@+id/string_name"
- [other attributes] >
+ android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
+ android:layout_width=["dimension" | "fill_parent" | "wrap_content"]
+ [View-specific attributes] >
<requestFocus/>
</View>
<ViewGroup >
diff --git a/docs/html/guide/topics/resources/menu-resource.jd b/docs/html/guide/topics/resources/menu-resource.jd
index c9f27fa71d250..badc40328a542 100644
--- a/docs/html/guide/topics/resources/menu-resource.jd
+++ b/docs/html/guide/topics/resources/menu-resource.jd
@@ -35,12 +35,12 @@ In XML: @[package:]menu.filename
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> - <item android:id="resource ID" + <item android:id="@+id/id_name" android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" android:title="string" android:titleCondensed="string" - android:icon="drawable resource" + android:icon="@[package:]drawable/drawable_resource_name" android:alphabeticShortcut="string" android:numericShortcut="string" android:checkable=["true" | "false"] @@ -172,22 +172,22 @@ too long.
res/menu/example_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:id="@+id/example_item"
- android:title="Example Item"
- android:icon="@drawable/example_item_icon" />
- <group android:id="@+id/example_group">
+ <item android:id="@+id/item1"
+ android:title="@string/item1"
+ android:icon="@drawable/group_item1_icon" />
+ <group android:id="@+id/group">
<item android:id="@+id/group_item1"
- android:title="Group Item 1"
- android:icon="@drawable/example_item1_icon" />
+ android:title="@string/group_item1"
+ android:icon="@drawable/group_item1_icon" />
<item android:id="@+id/group_item2"
- android:title="Group Item 2"
- android:icon="@drawable/example_item2_icon" />
+ android:title="G@string/group_item2"
+ android:icon="@drawable/group_item2_icon" />
</group>
<item android:id="@+id/submenu"
- android:title="Sub Menu" >
+ android:title="@string/submenu_title" >
<menu>
- <item android:id="@+id/submenu_item"
- android:title="Sub Menu Item" />
+ <item android:id="@+id/submenu_item1"
+ android:title="@string/submenu_item1" />
</menu>
</item>
</menu>
diff --git a/docs/html/guide/topics/resources/providing-resources.jd b/docs/html/guide/topics/resources/providing-resources.jd
index b495eb8a1b22c..0f3d389939589 100644
--- a/docs/html/guide/topics/resources/providing-resources.jd
+++ b/docs/html/guide/topics/resources/providing-resources.jd
@@ -7,13 +7,15 @@ parent.link=index.html
Quickview
- - Different types of resources belong in different sub-directories of {@code res/}
+ - Different types of resources belong in different subdirectories of {@code res/}
- Alternative resources provide configuration-specific resource files
In this document
+ - Grouping Resource Types
- Providing Alternative Resources
@@ -24,17 +26,30 @@ parent.link=index.html
There are several types of resource files you can -include in your application and each type belongs in a specific sub-directory of your project's -{@code res/} directory. Absolutely no files should be saved directly inside {@code res/}.
+You should always externalize application resources such as images and strings from your +code, so that you can maintain them independently. You can also provide alternative resources for +specific device configurations, by grouping them in specially-named resource directories. Android +will then automatically apply the appropriate resource based on the current configuration. For +instance, you might want to provide a different UI layout depending on the screen size.
-For example, here's the file hierarchy for a simple project:
+Once you save your resources external to your application code, you can access them +using resource IDs that are generated in your project's {@code R} class. How to use +resources in your application is discussed in Accessing +Resources.
-+ +Grouping Resource Types
+ +You should place each type of resource in a specific subdirectory of your project's +{@code res/} directory. For example, here's the file hierarchy for a simple project:
+ +MyProject/ src/ MyActivity.java @@ -42,23 +57,23 @@ MyProject/ drawable/ icon.png layout/ - main_layout.xml + main.xml + info.xml values/ strings.xml-This project includes an image resource, a layout resource, and string resource file.
+The {@code res/} directory contains all the resources (in subdirectories): an image resource, two +layout resources, and a string resource file. The resource directory names are important and are +described in table 1.
-Table 1 lists the different {@code res/} sub-directories supported -and describes the types of resource files that belong in each one.
- -Table 1. Resource directories. Each directory -belongs inside the project {@code res/} directory.
+Table 1. Resource directories +supported inside project {@code res/} directory.
| Directory | -Resource Types | +Resource Type |
|---|---|---|
color/ |
XML files that define a state list of colors. See Color -State List Resources | +State List Resource|
drawable/ |
Bitmap files ({@code .png}, {@code .9.png}, {@code .jpg}, {@code .gif}) or XML files that -are compiled into the following Drawable resource subtypes: +are compiled into the following drawable resource subtypes:
|
system. To open these resources with a raw {@link java.io.InputStream}, call {@link
android.content.res.Resources#openRawResource(int)
Resources.openRawResource()} with the resource ID, which is {@code R.raw.filename}.
- |
values/ |
XML files that contain simple values, such as strings, integers, and colors. -Unlike the other {@code res/} subdirectories, this one - can hold files that contain descriptions of more than one resource, rather than -just one resource for the file. The XML element types of an XML file in {@code values/} control -how these resources are defined in the {@code R} class. For example, a {@code <string>} -element will create an -{@code R.string} resource, and a {@code <color>} element will create an {@code R.color} + Whereas XML resource files in other {@code res/} subdirectories define a single resource +based on the XML filename, files in the {@code values/} directory describe multiple resources. +For a file in this directory, each child of the {@code <resources>} element defines a single +resource. For example, a {@code <string>} element creates an +{@code R.string} resource and a {@code <color>} element creates an {@code R.color} resource. -While the name of a file in this directory is arbitrary and not related to the name given -to a resource produced, you might want to separate different types of resources into different -files for easier maintenance. Here are some filename conventions for some of the different types -of resources you can save here: +Because each resource is defined with its own XML element, you can name the file +whatever you want and place different resource types in one file. However, for clarity, you might +want to place unique resource types in different files. For example, here are some filename +conventions for resources you can create in this directory:
See String Resources, Style Resource, and @@ -147,38 +160,42 @@ values. | |
xml/ |
- Arbitrary XML files that are compiled and can be read at runtime by calling {@link + | Arbitrary XML files that can be read at runtime by calling {@link android.content.res.Resources#getXml(int) Resources.getXML()}. Various XML configuration files -must also be saved here, such as a searchable configuration. |
Note: You should never save resource files directly inside the +{@code res/} directory.
+For more information about certain types of resources, see the Resource Types documentation.
- - +How to access resources in the {@code res/} subdirectories is discussed in Accessing Resources. +
Figure 1. Two device configurations, one using alternative resources.
Almost every application should provide alternative resources to support specific device -configurations. For instance, you should include different drawable resources for different -screen densities and different string resources for different languages. At runtime, Android -will automatically detect the current device configuration and then load the appropriate +configurations. For instance, you should include alternative drawable resources for different +screen densities and alternative string resources for different languages. At runtime, Android +automatically detects the current device configuration and loads the appropriate resources.
-For each set of resources for which you want to provide configuration-specific alternatives:
+To specify configuration-specific alternatives for a set of resources:
You can append more than one {@code <config_qualifier>}. Separate each one with a dash.
-For example, here are some default and alternative resources:
-+res/ drawable/ icon.png @@ -207,22 +224,23 @@ res/ background.png-The {@code hdpi} qualifier indicates that the resources are for devices with a high-density -screen. While the images in each directory are different, the filenames are -identical. This way, the resource ID that you use to reference the {@code icon.png} image is -always the same. When you request the {@code icon} drawable, Android will select the +
The {@code hdpi} qualifier indicates that the resources in that directory are for devices with a +high-density screen. While the images in each drawable directory are sized for a specific screen +density, the filenames are +the same. This way, the resource ID that you use to reference the {@code icon.png} or {@code +background.png} image is always the same, but Android selects the version of that drawable that best matches the current device configuration.
Android supports several configuration qualifiers and you can -add multiple qualifiers to one directory name in order to further specify the configuration, by -separating the qualifiers with dashes. Table 2 lists the valid configuration qualifiers, in order -of precedence—they must be specified in the directory name in the order that they are listed -in the table.
+add multiple qualifiers to one directory name, by separating each qualifier with a dash. Table 2 +lists the valid configuration qualifiers, in order of precedence—if you use multiple +qualifiers, they must be added to the directory name in the order they are listed in the +table.Table 2. Alternative resource qualifier names.
-