diff --git a/docs/html/guide/topics/resources/accessing-resources.jd b/docs/html/guide/topics/resources/accessing-resources.jd index e3e40550a57ac..cf8970c9b5ae9 100644 --- a/docs/html/guide/topics/resources/accessing-resources.jd +++ b/docs/html/guide/topics/resources/accessing-resources.jd @@ -22,12 +22,13 @@ parent.link=index.html

In this document

    -
  1. Accessing Resources in Code
  2. -
  3. Accessing Resources in Other XML Resources +
  4. Accessing Resources from Code
  5. +
  6. Accessing Resources from XML
    1. Referencing style attributes
  7. +
  8. Accessing Platform Resources

See also

@@ -39,154 +40,194 @@ parent.link=index.html -

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:

+ +

There are two ways you can access a resource:

+ -

Accessing Resources in Code

+

Accessing Resources in Code

-

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()}:

+
+ImageView imageView = (ImageView) findViewById(R.id.myimageview);
+imageView.setImageResource(R.drawable.myimage);
+
+

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()}.

-

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.

+

Syntax

-

Here is the syntax to reference a resource in code:

-

-[<package_name>.]R.<resource_type>.<resource_name> -

+

Here's the syntax to reference a resource in code:

+ +
+[<package_name>.]R.<resource_type>.<resource_name>
+

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}:

-
-ImageView iv = (ImageView) findViewById(R.id.myimageview);
-iv.setImageResource(R.drawable.myimage);
-
-

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:

-
-Resources res = this.getResources();
-String string = res.getString(R.string.mystring);
-
+

Use cases

-

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 -android package name. For example, -android.R.layout.simple_gallery_item.

+

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:

 // 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);
 
+

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.

+

Accessing Resources from XML

-

Accessing Resources in other XML Resources

+

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:

+ +
+<Button
+    android:layout_width="fill_parent"
+    android:layout_height="wrap_content"
+    android:text="@string/submit" />
+
+ + +

Syntax

Here is the syntax to reference a resource in an XML resource:

-

@[<package_name>:]<resource_type>/<resource_name>

+ +
+@[<package_name>:]<resource_type>/<resource_name>
+

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:

@@ -206,8 +247,8 @@ text string:

<EditText xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" - android:textColor="@color/opaque_red" - android:text="@string/hello" /> + android:textColor="@color/opaque_red" + android:text="@string/hello" />

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:

 <?xml version="1.0" encoding="utf-8"?>
@@ -239,15 +279,14 @@ image:

android:src="@drawable/other_drawable" />
-

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.

Referencing style attributes

-

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.

+ +

Accessing Platform Resources

+ +

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:fromXDelta
-
Float or percentage. Starting X offset. Either in: pixels relative to the -normal position, in percentage relative to the element width (%), or relative to the parent width -(%p).
+
Float or percentage. Starting X offset. Expressed either: in pixels relative +to the normal position (such as {@code "5"}), in percentage relative to the element width (such as +{@code "5%"}), or in percentage relative to the parent width (such as {@code "5%p"}).
android:toXDelta
-
Float or percentage. Ending X offset. Either in: pixels relative to the -normal position, in percentage relative to the element width (%), or relative to the parent width -(%p).
+
Float or percentage. Ending X offset. Expressed either: in pixels relative +to the normal position (such as {@code "5"}), in percentage relative to the element width (such as +{@code "5%"}), or in percentage relative to the parent width (such as {@code "5%p"}).
android:fromYDelta
-
Float or percentage. Starting Y offset. Either in: pixels relative to the -normal position, in percentage relative to the element height (%), or relative to the parent height -(%p).
+
Float or percentage. Starting Y offset. Expressed either: in pixels relative +to the normal position (such as {@code "5"}), in percentage relative to the element height (such as +{@code "5%"}), or in percentage relative to the parent height (such as {@code "5%p"}).
android:toYDelta
-
Float or percentage. Ending Y offset. Either in: pixels relative to the -normal position, in percentage relative to the element height (%), or relative to the parent height -(%p).
+
Float or percentage. Ending Y offset. Expressed either: in pixels relative +to the normal position (such as {@code "5"}), in percentage relative to the element height (such as +{@code "5%"}), or in percentage relative to the parent height (such as {@code "5%p"}).

For 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:fromDegrees
-
Integer. Starting angular position, in degrees.
+
Float. Starting angular position, in degrees.
android:toDegrees
-
Integer. Ending angular position, in degrees.
+
Float. Ending angular position, in degrees.
android:pivotX
-
Integer or percentage. The X coordinate of the center of rotation, in total -pixels (where 0 is the left edge) or percentage of the screen width.
+
Float or percentage. The X coordinate of the center of rotation. Expressed +either: in pixels relative to the object's left edge (such as {@code "5"}), in percentage relative +to the object's left edge (such as {@code "5%"}), or in percentage relative to the parent +container's left edge (such as {@code "5%p"}).
android:pivotY
-
Integer or percentage. The Y coordinate of the center of rotation, in total -pixels (where 0 is the top edge) or percentage of the screen height.
+
Float or percentage. The Y coordinate of the center of rotation. Expressed +either: in pixels relative to the object's top edge (such as {@code "5"}), in percentage relative +to the object's top edge (such as {@code "5%"}), or in percentage relative to the parent +container's top edge (such as {@code "5%p"}).

For 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:

+ + +
Animation Resources
Define pre-determined animations.
diff --git a/docs/html/guide/topics/resources/drawable-resource.jd b/docs/html/guide/topics/resources/drawable-resource.jd index ec964ed887d46..d8de16a429999 100644 --- a/docs/html/guide/topics/resources/drawable-resource.jd +++ b/docs/html/guide/topics/resources/drawable-resource.jd @@ -463,11 +463,11 @@ In XML: @[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 "
Specifies a gradient color for the shape.

attributes:

-
android:type
-
Keyword. The type of gradient pattern to apply. Valid values are: - - - - - - - - -
ValueDescription
{@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:angle
Integer. The angle for the gradient, in degrees. 0 is left to right, 90 is bottom to top. It must be a multiple of 45. Default is 0.
@@ -568,18 +556,30 @@ Does not apply when {@code android:type="linear"}.
android:centerColor
Color. Optional color that comes between the start and end colors, as a hexadecimal value or color resource.
-
android:gradientRadius
-
Float. The radius for the gradient. Only applied when {@code -android:type="radial"}.
-
android:useLevel
-
Boolean. "true" if this is used as a {@link -android.graphics.drawable.LevelListDrawable}.
-
android:startColor
-
Color. The starting color, as a hexadecimal -value or color resource.
android:endColor
Color. The ending color, as a hexadecimal value or color resource.
+
android:gradientRadius
+
Float. The radius for the gradient. Only applied when {@code +android:type="radial"}.
+
android:startColor
+
Color. The starting color, as a hexadecimal +value or color resource.
+
android:type
+
Keyword. The type of gradient pattern to apply. Valid values are: + + + + + + + + +
ValueDescription
{@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
+
Boolean. "true" if this is used as a {@link +android.graphics.drawable.LevelListDrawable}.
<solid>
diff --git a/docs/html/guide/topics/resources/index.jd b/docs/html/guide/topics/resources/index.jd index f602a0419c96b..2aa697e1402fb 100644 --- a/docs/html/guide/topics/resources/index.jd +++ b/docs/html/guide/topics/resources/index.jd @@ -27,14 +27,14 @@ important as more Android-powered devices become available with different config to provide this functionality, you must organize resources in your project's {@code res/} directory, using various sub-directories that group resources by type and configuration.

-
+

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.
 
XML file saved at 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

    +
  1. Grouping Resource Types
  2. Providing Alternative Resources
      +
    1. Qualifier name rules
    2. Creating alias resources
  3. @@ -24,17 +26,30 @@ parent.link=index.html
    1. Accessing Resources
    2. Resource Types
    3. +
    4. Supporting Multiple +Screens
-

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.

- + @@ -70,13 +85,13 @@ href="animation-resource.html">Animation Resources. +State List Resource 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}.

-

However, if you require direct access to original file names and file hierarchy, instead of -using a resource ID to access your files, you might consider saving some resources in the {@code -assets/} directory, instead of {@code res/raw/}. You can query data in the {@code assets/} directory -like an ordinary file system, search through the directory and read raw data using {@link -android.content.res.AssetManager}.

+

However, if you need access to original file names and file hierarchy, you might consider +saving some resources in the {@code +assets/} directory (instead of {@code res/raw/}). Files in {@code assets/} are not given a +resource ID, so you can read them only using {@link android.content.res.AssetManager}.

-
DirectoryResource TypesResource Type
color/ XML files that define a state list of colors. See Color -State List Resources
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:

  • Bitmap files
  • Nine-Patches (re-sizable bitmaps)
  • @@ -107,37 +122,35 @@ Menu. See Menu Resource.
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. +

Providing Alternative 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:

  1. Create a new directory in {@code res/} named in the form {@code <resources_name>-<config_qualifier>}. @@ -191,13 +208,13 @@ for which these resources are to be used.
  2. You can append more than one {@code <config_qualifier>}. Separate each one with a dash.

    -
  3. Save your alternative resources in this directory, named exactly the same as the default -resource files.
  4. +
  5. Save your alternative resources in this new directory. The resource files must be named +exactly the same as the default resource files.

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.

- +
@@ -237,18 +255,22 @@ names.

etc. +

If the device uses a radio connection (GSM phone), the MCC comes + from the SIM, and the MNC comes from the network to which the + device is connected.

+

You can also use the MCC alone (for example, to include country-specific legal +resources in your application). If you need to specify based on the language only, then use the +language and region qualifier instead (discussed next). If you decide to use the MCC and +MNC qualifier, you should do so with care and test that it works as expected.

+

Also see the configuration fields {@link +android.content.res.Configuration#mcc}, and {@link +android.content.res.Configuration#mnc}, which indicate the current mobile country code +and mobile network code, respectively.

+ @@ -260,22 +282,24 @@ should do so with great care and completely test that it works as expected.

< fr-rCA
etc. - @@ -286,24 +310,27 @@ your application for other langauges.

large @@ -313,12 +340,14 @@ Screens for more information.

notlong @@ -329,11 +358,16 @@ Screens for more information.

square --> @@ -343,12 +377,15 @@ how this affects your application during runtime.

desk @@ -358,13 +395,16 @@ android.app.UiModeManager}.

notnight @@ -376,24 +416,27 @@ also explicitly set this mode using {@link android.app.UiModeManager}.

nodpi @@ -403,25 +446,45 @@ Screens for more information.

stylus
finger - @@ -431,9 +494,17 @@ application during runtime.

qwerty
12key - + @@ -442,13 +513,16 @@ the {@code qwerty} resources.navhidden @@ -459,8 +533,16 @@ information about how this affects your application during runtime.

trackball
wheel -
Qualifier Values -

Specifies resources based on the mobile country code (MCC), optionally followed by mobile -network code (MNC) - from the SIM in the device. For example, mcc310 is U.S. on any carrier, +

The mobile country code (MCC), optionally followed by mobile network code (MNC) + from the SIM card in the device. For example, mcc310 is U.S. on any carrier, mcc310-mnc004 is U.S. on Verizon, and mcc208-mnc00 is France on Orange.

-

If the device uses a radio connection (GSM phone), the MCC will come - from the SIM, and the MNC will come from the network to which the - device is attached.

-

You might sometimes use the MCC alone, for example to include country-specific legal -resources in your application, but if you only need to specify based on language, then use the -language and region qualifier below. If you decide to use the MCC and MNC qualifier, you -should do so with great care and completely test that it works as expected.

Language and region

This is defined by a two-letter

The language is defined by a two-letter ISO 639-1 language code, optionally followed by a two letter ISO - 3166-1-alpha-2 region code (preceded by lowercase "r"). + 3166-1-alpha-2 region code (preceded by lowercase "{@code r}").

The codes are not case-sensitive; the {@code r} prefix is used to distinguish the region portion. You cannot specify a region alone.

This can change during the life -of your application if the user changes their language in the system settings. See Handling Runtime Changes for information about how this can affect your application during runtime.

See Localization for a complete guide to localizing your application for other langauges.

+

Also see the {@link android.content.res.Configuration#locale} configuration field, which +indicates the current locale.

-
    -
  • Small screens are based on the space available on a - QVGA low density screen. Considering a portrait HVGA display, this has - the same available width but less height -- it is 3:4 vs. HVGA's +
      +
    • {@code small}: Screens based on the space available on a + low-density QVGA screen. Considering a portrait HVGA display, this has + the same available width but less height—it is 3:4 vs. HVGA's 2:3 aspect ratio. Examples are QVGA low density and VGA high density.
    • -
    • Normal screens are based on the traditional Android HVGA - medium density screen. A screen is considered to be normal if it is +
    • {@code normal}: Screens based on the traditional + medium-density HVGA screen. A screen is considered to be normal if it is at least this size (independent of density) and not larger. Examples of such screens a WQVGA low density, HVGA medium density, WVGA high density.
    • -
    • Large screens are based on the space available on a - VGA medium density screen. Such a screen has significantly more +
    • {@code large}: Screens based on the space available on a + medium-density VGA screen. Such a screen has significantly more available space in both width and height than an HVGA display. Examples are VGA and WVGA medium density screens.

    See Supporting Multiple Screens for more information.

    +

    Also see the {@link android.content.res.Configuration#screenLayout} configuration field, +which indicates whether the screen is small, normal, +or large.

-

This is based purely on the aspect ratio of the screen (a "long" screen is wider): -

    -
  • Long: WQVGA, WVGA, FWVGA
  • -
  • Not long: QVGA, HVGA, and VGA
  • +
      +
    • {@code long}: Long screens, such as WQVGA, WVGA, FWVGA
    • +
    • {@code notlong}: Not long screens, such as QVGA, HVGA, and VGA
    -

    This is not related to the screen orientation.

    +

    This is based purely on the aspect ratio of the screen (a "long" screen is wider). This +is not related to the screen orientation.

    +

    Also see the {@link android.content.res.Configuration#screenLayout} configuration field, +which indicates whether the screen is long.

-

Portrait orientation ({@code port}) is vertical and landscape orientation -({@code land}) is horizontal.

+
    +
  • {@code port}: Device is in portrait orientation (vertical)
  • +
  • {@code land}: Device is in landscape orientation (horizontal)
  • + +

This can change during the life of your application if the user rotates the screen. See Handling Runtime Changes for information about how this affects your application during runtime.

+

Also see the {@link android.content.res.Configuration#orientation} configuration field, +which indicates the current device orientation.

-

These configurations can be initiated when the device is placed in a dock.

+
    +
  • {@code car}: Device is in a car dock
  • +
  • {@code desk}: Device is in a desk dock
  • +

Added in API Level 8.

This can change during the life of your application if the user places the device in a -dock. See Handling Runtime Changes for -information about how this affects your application during runtime. Also see {@link -android.app.UiModeManager}.

+dock. You can eneable or disable this mode using {@link +android.app.UiModeManager}. See Handling Runtime Changes for +information about how this affects your application during runtime.

-

- These configurations can be initiated by the device light sensor (if available).

+
    +
  • {@code night}: Night time
  • +
  • {@code notnight}: Day time
  • +

Added in API Level 8.

-

This can change during the life of your application if the device determines that the -user environment is a "night" (dark) setting. See Handling Runtime -Changes for information about how this affects your application during runtime. You can -also explicitly set this mode using {@link android.app.UiModeManager}.

+

This can change during the life of your application if night mode is left in +auto mode (default), in which case the mode changes based on the time of day. You can eneable +or disable this mode using {@link android.app.UiModeManager}. See Handling Runtime Changes for information about how this affects your +application during runtime.

-

The medium - density of traditional HVGA screens (mdpi) is defined to be approximately - 160dpi; low density (ldpi) is 120, and high density (hdpi) is 240. There - is thus a 4:3 scaling factor between each density, so a 9x9 bitmap - in ldpi would be 12x12 in mdpi and 16x16 in hdpi. The special - nodpi density can be used with bitmap resources to prevent - them from being scaled at load time to match the device density. -

- When Android selects which resource files to use, - it handles screen density differently than the other qualifiers. +

    +
  • {@code ldpi}: Low-density screens; approximately 120dpi.
  • +
  • {@code mdpi}: Medium-density (on traditional HVGA) screens; approximately +160dpi.
  • +
  • {@code hdpi}: High-density screens; approximately 240dpi.
  • +
  • {@code nodpi}: This can be used for bitmap resources that you do not want to be scaled +to match the device density.
  • +
+

There is thus a 4:3 scaling factor between each density, so a 9x9 bitmap + in ldpi is 12x12 in mdpi and 16x16 in hdpi.

+

When Android selects which resource files to use, + it handles screen density differently than the other qualifiers. In step 1 of How Android finds the best matching directory (below), screen density is always considered to be a match. In step 4, if the qualifier being considered is screen - density, Android will select the best final match at that point, + density, Android selects the best final match at that point, without any need to move on to step 5.

See Supporting Multiple -Screens for more information.

+Screens for more information about how to handle screen sizes and how Android might scale +your bitmaps.

If the device has a resistive touch screen that's suited for use with a stylus, -then it may use the {@code stylus} resources.

+
+
    +
  • {@code notouch}: Device does not have a touchscreen.
  • +
  • {@code stylus}: Device has a resistive touchscreen that's suited for use with a +stylus.
  • +
  • {@code finger}: Device has a touchscreen.
  • +
+

Also see the {@link android.content.res.Configuration#touchscreen} configuration field, +which indicates the type of touchscreen on the device.

Keyboard availability keysexposed
- keyshidden
keyssoft
-

If your application has specific resources that should only be used with a soft keyboard, -use the keyssoft value. If you do not provide keyssoft resources, but do -provide keysexposed and keyshidden, and the device shows a soft keyboard, -the system will use keysexposed resources.

-

This can change during the life of your application if the user opens a keyboard. See Handling Runtime Changes for information about how this affects your -application during runtime.

+
    +
  • {@code keysexposed}: Device has a keyboard available. If the device has a +software keyboard enabled (which is likely), this may be used even when the hardware keyboard is +not exposed to the user, even if the device has no hardware keyboard. If no software +keyboard is provided or it's disabled, then this is only used when a hardware keyboard is +exposed.
  • +
  • {@code keyshidden}: Device has a hardware keyboard available but it is +hidden and the device does not have a software keyboard enabled.
  • +
  • {@code keyssoft}: Device has a software keyboard enabled, whether it's +visible or not.
  • +
+

If you provide keysexposed resources, but not keyssoft +resources, the system uses the keysexposed resources regardless of whether a +keyboard is visible, as long as the system has a software keyboard enabled.

+

This can change during the life of your application if the user opens a hardware +keyboard. See Handling Runtime Changes for information about how +this affects your application during runtime.

+

Also see the configuration fields {@link +android.content.res.Configuration#hardKeyboardHidden} and {@link +android.content.res.Configuration#keyboardHidden}, which indicate the visibility of a hardware +keyboard and and the visibility of any kind of keyboard (including software), respectively.

If the device has no hardware keys for text input, then it may use the {@code -nokeys} resources. Even if the device has a QWERTY keyboard but it is currently hidden, it may use -the {@code qwerty} resources.

+
    +
  • {@code nokeys}: Device has no hardware keys for text input.
  • +
  • {@code qwert}: Device has a hardware qwerty keyboard, whether it's visible to the user +or not.
  • +
  • {@code 12key}: Device has a hardware 12-key keyboard, whether it's visible to the user +or not.
  • +
+

Also see the {@link android.content.res.Configuration#keyboard} configuration field, +which indicates the primary text input method available.

+
Navigation key availability -

- If the device's navigation keys are currently available to - the user, it may use the {@code navexposed} resources; if they are not - available (such as behind a closed lid), it may use the {@code navhidden} resources.

+
    +
  • {@code navexposed}: Navigation keys are available to the user.
  • +
  • {@code navhidden}: Navigation keys are not available (such as behind a closed +lid).
  • +

This can change during the life of your application if the user reveals the navigation keys. See Handling Runtime Changes for information about how this affects your application during runtime.

+

Also see the {@link android.content.res.Configuration#navigationHidden} configuration +field, which indicates whether navigation keys are hidden.

If the device has no navigation facility other than using the touchscreen, then it -may use the {@code nonav} resources.

+
+
    +
  • {@code nonav}: Device has no navigation facility other than using the +touchscreen.
  • +
  • {@code dpad}: Device has a directional-pad (d-pad) for navigation.
  • +
  • {@code trackball}: Device has a trackball for navigation.
  • +
  • {@code wheel}: Device has a directional wheel(s) for navigation (uncommon).
  • +
+

Also see the {@link android.content.res.Configuration#navigation} configuration field, +which indicates the type of navigation method available.