From f940a1f316ddbed760f6f3ab9a3e4f2112909381 Mon Sep 17 00:00:00 2001 From: Scott Main Date: Tue, 9 Feb 2010 18:48:27 -0800 Subject: [PATCH] cherry-pick from master: Icf079f5f45b1745a8d54f504e28dbbb52c6f7c96 docs: rewrite resources documentation Huge overhaul. Includes all new dev guides about how resources are used on Android and how to provide alternatives, including how to handle runtime configuration changes (restarts). Plus all new reference docs for all the resource types (drawables, strings, menus, etc.). Change-Id: I12e819d2c5fc11e062281d8fe442c3037e92000a --- docs/html/guide/guide_toc.cs | 28 +- .../html/guide/topics/graphics/2d-graphics.jd | 6 +- .../topics/resources/accessing-resources.jd | 284 ++++ .../topics/resources/animation-resource.jd | 564 +++++++ .../topics/resources/available-resources.jd | 1472 +---------------- .../topics/resources/color-list-resource.jd | 165 ++ .../topics/resources/drawable-resource.jd | 711 ++++++++ docs/html/guide/topics/resources/index.jd | 120 +- .../guide/topics/resources/layout-resource.jd | 224 +++ .../guide/topics/resources/localization.jd | 55 +- .../guide/topics/resources/menu-resource.jd | 207 +++ .../guide/topics/resources/more-resources.jd | 684 ++++++++ .../topics/resources/providing-resources.jd | 723 ++++++++ .../guide/topics/resources/resources-i18n.jd | 810 +-------- .../guide/topics/resources/runtime-changes.jd | 245 +++ .../guide/topics/resources/string-resource.jd | 444 +++++ .../guide/topics/resources/style-resource.jd | 128 ++ .../resources/resource_devices_diagram1.png | Bin 0 -> 29222 bytes .../resources/resource_devices_diagram2.png | Bin 0 -> 32274 bytes 19 files changed, 4571 insertions(+), 2299 deletions(-) create mode 100644 docs/html/guide/topics/resources/accessing-resources.jd create mode 100644 docs/html/guide/topics/resources/animation-resource.jd create mode 100644 docs/html/guide/topics/resources/color-list-resource.jd create mode 100644 docs/html/guide/topics/resources/drawable-resource.jd create mode 100644 docs/html/guide/topics/resources/layout-resource.jd create mode 100644 docs/html/guide/topics/resources/menu-resource.jd create mode 100644 docs/html/guide/topics/resources/more-resources.jd create mode 100644 docs/html/guide/topics/resources/providing-resources.jd create mode 100644 docs/html/guide/topics/resources/runtime-changes.jd create mode 100644 docs/html/guide/topics/resources/string-resource.jd create mode 100644 docs/html/guide/topics/resources/style-resource.jd create mode 100644 docs/html/images/resources/resource_devices_diagram1.png create mode 100644 docs/html/images/resources/resource_devices_diagram2.png diff --git a/docs/html/guide/guide_toc.cs b/docs/html/guide/guide_toc.cs index dce78c4b3ecbc..fd163f6b2ba26 100644 --- a/docs/html/guide/guide_toc.cs +++ b/docs/html/guide/guide_toc.cs @@ -99,18 +99,36 @@
  • - Resources and Assets + Application Resources
  • diff --git a/docs/html/guide/topics/graphics/2d-graphics.jd b/docs/html/guide/topics/graphics/2d-graphics.jd index 051427b56ae56..e46dbb46a3cb7 100644 --- a/docs/html/guide/topics/graphics/2d-graphics.jd +++ b/docs/html/guide/topics/graphics/2d-graphics.jd @@ -442,7 +442,7 @@ class is the basis for frame animations.

    While you can define the frames of an animation in your code, using the {@link android.graphics.drawable.AnimationDrawable} class API, it's more simply accomplished with a single XML file that lists the frames that compose the animation. Like the tween animation above, the XML file for this kind -of animation belongs in the res/anim/ directory of your Android project. In this case, +of animation belongs in the res/drawable/ directory of your Android project. In this case, the instructions are the order and duration for each frame of the animation.

    The XML file consists of an <animation-list> element as the root node and a series @@ -459,7 +459,7 @@ Here's an example XML file for a frame-by-frame animation:

    This animation runs for just three frames. By setting the android:oneshot attribute of the list to true, it will cycle just once then stop and hold on the last frame. If it is set false then -the animation will loop. With this XML saved as rocket_thrust.xml in the res/anim/ directory +the animation will loop. With this XML saved as rocket_thrust.xml in the res/drawable/ directory of the project, it can be added as the background image to a View and then called to play. Here's an example Activity, in which the animation is added to an {@link android.widget.ImageView} and then animated when the screen is touched:

    @@ -470,7 +470,7 @@ public void onCreate(Bundle savedInstanceState) {
       setContentView(R.layout.main);
     
       ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
    -  rocketImage.setBackgroundResource(R.anim.rocket_thrust);
    +  rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
       rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
     }
     
    diff --git a/docs/html/guide/topics/resources/accessing-resources.jd b/docs/html/guide/topics/resources/accessing-resources.jd
    new file mode 100644
    index 0000000000000..e3e40550a57ac
    --- /dev/null
    +++ b/docs/html/guide/topics/resources/accessing-resources.jd
    @@ -0,0 +1,284 @@
    +page.title=Accessing Resources
    +parent.title=Application Resources
    +parent.link=index.html
    +@jd:body
    +
    +
    +
    +
    +

    There are two ways you can reference your resources for use in your application:

    +
      +
    • From your code: Using an integer from a sub-class in your {@code R} class, +such as: +

      {@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).

      +
    • +
    • From another resource: Using a special XML syntax that corresponds to the +{@code R} sub-class, such as: +

      {@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.

      +
    • +
    + + + +

    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.

    + + + + + +

    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:

    +

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

    + +
      +
    • {@code <package_name>} is the name of the package in which the resource is located (not +required when referencing resources from your own package).
    • +
    • {@code <resource_type>} is the {@code R} subclass for the resource type.
    • +
    • {@code <resource_name>} is either the {@code +android:name} attribute value (for some resources defined in XML files) or the resource filename +without the extension.
    • +
    +

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

    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.

    + +

    Here are some examples of using 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) ;
    +
    +// 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));
    +
    +// Load a custom layout for the current screen
    +{@link android.app.Activity#setContentView(int)
    +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));
    +
    +// 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);
    +
    + + + + + + +

    Accessing Resources in other XML Resources

    + +

    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.

    + +

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

    +

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

    + +
      +
    • {@code <package_name>} is the name of the package in which the resource is located (not +required when referencing resources from the same package)
    • +
    • {@code <resource_type>} is the +{@code R} subclass for the resource type
    • +
    • {@code <resource_name>} is either the {@code +android:name} attribute value (for some resources defined in XML files) or the resource filename +without the extension
    • +
    + +

    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 color resource and a string resource:

    + +
    +<?xml version="1.0" encoding="utf-8"?>
    +<resources>
    +   <color name="opaque_red">#f00</color>
    +   <string name="hello">Hello!</string>
    +</resources>
    +
    + +

    You can use these resources in the following layout file to set the text color and +text string:

    + +
    +<?xml version="1.0" encoding="utf-8"?>
    +<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" />
    +
    + +

    In this case you don't need to specify the package name in the resource reference because the +resources are from your own package. To +reference a system resource, you would need to include the package name. For example:

    + +
    +<?xml version="1.0" encoding="utf-8"?>
    +<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: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 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:

    + +
    +<?xml version="1.0" encoding="utf-8"?>
    +<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    +    android:src="@drawable/other_drawable" />
    +
    + +

    This is discussed further in Creating +alias resources.

    + + + + +

    Referencing style attributes

    + +

    A style attribute resource is another type of resource that 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 +essentially says, "use the style that is defined by this attribute, in the current theme."

    + +

    To reference a style attribute, the name syntax is almost identical to the normal resource +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:

    + +
    +<EditText id="text"
    +    android:layout_width="fill_parent"
    +    android:layout_height="wrap_content"
    +    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, +you do not need to explicitly state the type (which would be +?android:attr/textColorSecondary), so you can exclude the {@code attr} type.

    + + + diff --git a/docs/html/guide/topics/resources/animation-resource.jd b/docs/html/guide/topics/resources/animation-resource.jd new file mode 100644 index 0000000000000..b2fab04e4aab0 --- /dev/null +++ b/docs/html/guide/topics/resources/animation-resource.jd @@ -0,0 +1,564 @@ +page.title=Animation Resources +parent.title=Resource Types +parent.link=available-resources.html +@jd:body + +
    +
    +

    See also

    +
      +
    1. 2D +Graphics
    2. +
    +
    +
    + + +

    An animation resource can define one of two types of animations:

    +
    +
    Tween Animation
    +
    Creates an animation by performing a series of transformations on a single image. + An {@link android.view.animation.Animation}.
    +
    Frame Animation
    +
    Creates an animation by showing a sequence of images in order. + An {@link android.graphics.drawable.AnimationDrawable}.
    +
    + + + +

    Tween Animation

    + +

    An animation defined in XML that performs transitions such as rotating, +fading, moving, and stretching on a graphic. +

    + +
    + +
    file location:
    +
    res/anim/filename.xml
    +The filename will be used as the resource ID.
    + +
    compiled resource datatype:
    +
    Resource pointer to an {@link android.view.animation.Animation}.
    + +
    resource reference:
    +
    +In Java: R.anim.filename
    +In XML: @[package:]anim/filename +
    + +
    syntax:
    +
    +
    +<?xml version="1.0" encoding="utf-8"?>
    +<set xmlns:android="http://schemas.android.com/apk/res/android"
    +    android:interpolator="@[package:]anim/interpolator_resource"
    +    android:shareInterpolator=["true" | "false"] >
    +    <alpha
    +        android:fromAlpha="float"
    +        android:toAlpha="float" />
    +    <scale
    +        android:fromXScale="float"
    +        android:toXScale="float"
    +        android:fromYScale="float"
    +        android:toYScale="float"
    +        android:pivotX="string"
    +        android:pivotY="string" />
    +    <translate
    +        android:fromX="string"
    +        android:toX="string"
    +        android:fromY="string"
    +        android:toY="string" />
    +    <rotate
    +        android:fromDegrees="float"
    +        android:toDegrees="float"
    +        android:pivotX="string"
    +        android:pivotY="string" />
    +    <set>
    +        ...
    +    </set>
    +</set>
    +
    + +

    The file must have a single root element: either an +<alpha>, <scale>, <translate>, +<rotate>, or <set> element that holds +a group (or groups) of other animation elements (even nested <set> elements). +

    +
    + +
    elements:
    +
    +
    +
    <set>
    +
    A container that holds other animation elements +(<alpha>, <scale>, <translate>, +<rotate>) or other <set> elements. Represents an {@link +android.view.animation.AnimationSet}. +

    attributes:

    +
    +
    android:interpolator
    +
    Interpolator resource. + An {@link android.view.animation.Interpolator} to apply on the animation. + The value must be a reference to a resource that specifies an interpolator + (not an interpolator class name). There are default interpolator + resources available from the platform or you can create your own interpolator resource. + See the discussion below for more about Interpolators.
    +
    android:shareInterpolator
    +
    Boolean. "true" if you want to share the same interpolator among all child +elements.
    +
    +
    +
    <alpha>
    +
    A fade-in or fade-out animation. Represents an {@link +android.view.animation.AlphaAnimation}. +

    attributes:

    +
    +
    android:fromAlpha
    +
    Float. Starting opacity offset, where 0.0 is transparent and 1.0 +is opaque.
    +
    android:toAlpha
    +
    Float. Ending opacity offset, where 0.0 is transparent and 1.0 +is opaque.
    +
    +

    For more attributes supported by <alpha>, see the +{@link android.view.animation.Animation} class reference (of which, all XML attributes are +inherrited by this element).

    +
    +
    <scale>
    +
    A resizing animation. You can specify the center point of the image from which it grows +outward (or inward) by specifying {@code pivotX} and {@code pivotY}. For example, if these values +are 0, 0 (top-left corner), all growth will be down and to the right. Represents a {@link +android.view.animation.ScaleAnimation}. +

    attributes:

    +
    +
    android:fromXScale
    +
    Float. Starting X size offset, where 1.0 is no change.
    +
    android:toXScale
    +
    Float. Ending X size offset, where 1.0 is no change.
    +
    android:fromYScale
    +
    Float. Starting Y size offset, where 1.0 is no change.
    +
    android:toYScale
    +
    Float. Ending Y size offset, where 1.0 is no change.
    +
    android:pivotX
    +
    Float. The X coordinate to remain fixed when the object is scaled.
    +
    android:pivotY
    +
    Float. The Y coordinate to remain fixed when the object is scaled.
    +
    +

    For more attributes supported by <scale>, see the +{@link android.view.animation.Animation} class reference (of which, all XML attributes are +inherrited by this element).

    +
    +
    <translate>
    +
    A vertical and/or horizontal motion. Supports the following attributes in any of +the following three formats: values from -100 to 100 ending with "%", indicating a percentage +relative to itself; values from -100 to 100 ending in "%p", indicating a percentage relative to its +parent; a float value with no suffix, indicating an absolute value. Represents a {@link +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).
    +
    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).
    +
    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).
    +
    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).
    +
    +

    For more attributes supported by <translate>, see the +{@link android.view.animation.Animation} class reference (of which, all XML attributes are +inherrited by this element).

    +
    +
    <rotate>
    +
    A rotation animation. Represents a {@link android.view.animation.RotateAnimation}. +

    attributes:

    +
    +
    android:fromDegrees
    +
    Integer. Starting angular position, in degrees.
    +
    android:toDegrees
    +
    Integer. 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.
    +
    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.
    +
    +

    For more attributes supported by <rotate>, see the +{@link android.view.animation.Animation} class reference (of which, all XML attributes are +inherrited by this element).

    +
    +
    +
    + +
    example:
    +
    + XML file saved at res/anim/hyperspace_jump.xml:

    +
    +<set xmlns:android="http://schemas.android.com/apk/res/android"
    +    android:shareInterpolator="false">
    +    <scale
    +        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    +        android:fromXScale="1.0" 
    +        android:toXScale="1.4" 
    +        android:fromYScale="1.0" 
    +        android:toYScale="0.6" 
    +        android:pivotX="50%"
    +        android:pivotY="50%"
    +        android:fillAfter="false"
    +        android:duration="700" />
    +    <set
    +        android:interpolator="@android:anim/accelerate_interpolator"
    +        android:startOffset="700">
    +        <scale
    +            android:fromXScale="1.4" 
    +            android:toXScale="0.0"
    +            android:fromYScale="0.6"
    +            android:toYScale="0.0" 
    +            android:pivotX="50%" 
    +            android:pivotY="50%" 
    +            android:duration="400" />
    +        <rotate
    +            android:fromDegrees="0" 
    +            android:toDegrees="-45"
    +            android:toYScale="0.0" 
    +            android:pivotX="50%" 
    +            android:pivotY="50%"
    +            android:duration="400" />
    +    </set>
    +</set>
    +
    +

    This application code will apply the animation to an {@link android.widget.ImageView} and +start the animation:

    +
    +ImageView image = (ImageView) findViewById(R.id.image);
    +Animation hyperspaceJump = AnimationUtils.{@link android.view.animation.AnimationUtils#loadAnimation(Context,int) loadAnimation}(this, R.anim.hyperspace_jump);
    +image.{@link android.view.View#startAnimation(Animation) startAnimation}(hyperspaceJump);
    +
    +
    + + +
    see also:
    +
    + +
    + +
    + + + + + +

    Interpolators

    + +

    An interpolator is an animation modifier defined in XML that affects the rate of change in an +animation. This allows your existing animation effects to be accelerated, decelerated, repeated, +bounced, etc.

    + +

    An interpolator is applied to an animation element with the {@code android:interpolator} +attribute, the value of which is a reference to an interpolator resource.

    + +

    All interpolators available in Android are subclasses of the {@link +android.view.animation.Interpolator} class. For each interpolator class, Android +includes a public resource you can reference in order to apply the interpolator to an animation +using the the {@code android:interpolator} attribute. +The following table specifies the resource to use for each interpolator:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Interpolator classResource ID
    {@link android.view.animation.AccelerateDecelerateInterpolator}{@code @android:anim/accelerate_decelerate_interpolator}
    {@link android.view.animation.AccelerateInterpolator}{@code @android:anim/accelerate_interpolator}
    {@link android.view.animation.AnticipateInterpolator}{@code @android:anim/anticipate_interpolator}
    {@link android.view.animation.AnticipateOvershootInterpolator}{@code @android:anim/anticipate_overshoot_interpolator}
    {@link android.view.animation.BounceInterpolator}{@code @android:anim/bounce_interpolator}
    {@link android.view.animation.CycleInterpolator}{@code @android:anim/cycle_interpolator}
    {@link android.view.animation.DecelerateInterpolator}{@code @android:anim/decelerate_interpolator}
    {@link android.view.animation.LinearInterpolator}{@code @android:anim/linear_interpolator}
    {@link android.view.animation.OvershootInterpolator}{@code @android:anim/overshoot_interpolator}
    + +

    Here's how you can apply one of these with the {@code android:interpolator} attribute:

    +
    +<set android:interpolator="@android:anim/accelerate_interpolator">
    +    ...
    +</set>
    +
    + + +

    Custom interpolators

    + +

    If you're not satisfied with the interpolators provided by the platform (listed in the +table above), you can create a custom interpolator resource with modified attributes. +For example, you can adjust the rate of +acceleration for the {@link android.view.animation.AnticipateInterpolator}, or adjust the number of +cycles for the {@link android.view.animation.CycleInterpolator}. In order to do so, you need to +create your own interpolator resource in an XML file. +

    + +
    + +
    file location:
    +
    res/anim/filename.xml
    +The filename will be used as the resource ID.
    + +
    compiled resource datatype:
    +
    Resource pointer to the corresponding interpolator object.
    + +
    resource reference:
    +
    +In XML: @[package:]anim/filename +
    + +
    syntax:
    +
    +
    +<?xml version="1.0" encoding="utf-8"?>
    +<InterpolatorName xmlns:android="http://schemas.android.com/apk/res/android"
    +    android:attribute_name="value"
    +    />
    +
    +

    If you don't apply any attributes, then your interpolator will function exactly the same as +those provided by the platform (listed in the table above).

    +
    + +
    elements:
    +
    Notice that each {@link android.view.animation.Interpolator} implementation, when +defined in XML, begins its name in lowercase.

    + +
    +
    <accelerateDecelerateInterpolator>
    +
    The rate of change starts and ends slowly but accelerates through the +middle.

    No attributes.

    +
    <accelerateInterpolator>
    +
    The rate of change starts out slowly, then accelerates. +

    attributes:

    +
    +
    android:factor
    +
    Float. The acceleration rate (default is 1).
    +
    +
    +
    <anticipateInterpolator>
    +
    The change starts backward then flings forward. +

    attributes:

    +
    +
    android:tension
    +
    Float. The amount of tension to apply (default is 2).
    +
    +
    +
    <anticipateOvershootInterpolator>
    +
    The change starts backward, flings forward and overshoots the target value, then +settles at the final value. +

    attributes:

    +
    +
    android:tension
    +
    Float. The amount of tension to apply (default is 2).
    +
    android:extraTension
    +
    Float. The amount by which to multiply the tension (default is + 1.5).
    +
    +
    +
    <bounceInterpolator>
    +
    The change bounces at the end.

    No attributes

    +
    <cycleInterpolator>
    +
    Repeats the animation for a specified number of cycles. The rate of change follows a +sinusoidal pattern. +

    attributes:

    +
    +
    android:cycles
    +
    Integer. The number of cycles (default is 1).
    +
    +
    +
    <decelerateInterpolator>
    +
    The rate of change starts out quickly, then decelerates. +

    attributes:

    +
    +
    android:factor
    +
    Float. The deceleration rate (default is 1).
    +
    +
    +
    <linearInterpolator>
    +
    The rate of change is constant.

    No attributes.

    +
    <overshootInterpolator>
    +
    The change flings forward and overshoots the last value, then comes back. +

    attributes:

    +
    +
    android:tension
    +
    Float. The amount of tension to apply (default is 2).
    +
    +
    +
    + +
    example:
    +
    +

    XML file saved at res/anim/my_overshoot_interpolator.xml:

    +
    +<?xml version="1.0" encoding="utf-8"?>
    +<overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    +    android:tension="7.0"
    +    />
    +
    +

    This animation XML will apply the interpolator:

    +
    +<scale xmlns:android="http://schemas.android.com/apk/res/android"
    +    android:interpolator="@anim/my_overshoot_interpolator"
    +    android:fromXScale="1.0"
    +    android:toXScale="3.0"
    +    android:fromYScale="1.0"
    +    android:toYScale="3.0"
    +    android:pivotX="50%"
    +    android:pivotY="50%"
    +    android:duration="700" />
    +
    +
    +
    + + + + + + + + + + + + + + + + + +

    Frame Animation

    + +

    An animation defined in XML that shows a sequence of images in order (like a film). +

    + + +
    + +
    file location:
    +
    res/drawable/filename.xml
    +The filename will be used as the resource ID.
    + +
    compiled resource datatype:
    +
    Resource pointer to an {@link android.graphics.drawable.AnimationDrawable}.
    + +
    resource reference:
    +
    +In Java: R.drawable.filename
    +In XML: @[package:]drawable.filename +
    + +
    syntax:
    +
    +
    +<?xml version="1.0" encoding="utf-8"?>
    +<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    +    android:oneshot=["true" | "false"] >
    +    <item
    +        android:drawable="@[package:]drawable/drawable_resource_name"
    +        android:duration="integer" />
    +</animation-list>
    +
    +
    + +
    elements:
    +
    +
    +
    <animation-list>
    +
    Required. This must be the root element. Contains one or more +<item> elements. +

    attributes:

    +
    +
    android:oneshot
    +
    Boolean. "true" if you want to perform the animation once; "false" to loop the +animation.
    +
    +
    +
    <item>
    +
    A single frame of animation. Must be a child of a <animation-list> element. +

    attributes:

    +
    +
    android:drawable
    +
    Drawable resource. The drawable to use for this frame.
    +
    android:duration
    +
    Integer. The duration to show this frame, in milliseconds.
    +
    +
    +
    +
    + +
    example:
    +
    +
    +
    XML file saved at res/anim/rocket.xml:
    +
    +
    +<?xml version="1.0" encoding="utf-8"?>
    +<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    +    android:oneshot="false">
    +    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    +    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    +    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
    +</animation-list>
    +
    +
    + +
    This application code will set the animation as the background for a View, + then play the animation:
    +
    +
    +ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
    +rocketImage.{@link android.view.View#setBackgroundResource(int) setBackgroundResource}(R.drawable.rocket_thrust);
    +
    +rocketAnimation = (AnimationDrawable) rocketImage.{@link android.view.View#getBackground()};
    +rocketAnimation.{@link android.graphics.drawable.AnimationDrawable#start()};
    +
    +
    + +
    +
    + +
    + + + diff --git a/docs/html/guide/topics/resources/available-resources.jd b/docs/html/guide/topics/resources/available-resources.jd index 0e003a0eb3cea..19babee6c9220 100644 --- a/docs/html/guide/topics/resources/available-resources.jd +++ b/docs/html/guide/topics/resources/available-resources.jd @@ -1,1451 +1,59 @@ -page.title=Available Resource Types -parent.title=Resources and Assets +page.title=Resource Types +parent.title=Application Resources parent.link=index.html @jd:body
    - -

    Key classes

    +

    See also

      -
    1. {@link android.content.res.Resources}
    2. -
    3. {@link android.content.res.AssetManager}
    4. +
    5. Providing Resources
    6. +
    7. Accessing Resources
    - -

    In this document

    -
      -
    1. Simple Values -
        -
      1. Color Values
      2. -
      3. Strings and Styled Text
      4. -
      5. Dimension Values
      6. -
      -
    2. -
    3. Drawables -
        -
      1. Bitmap Files
      2. -
      3. Color Drawables
      4. -
      5. Nine-Patch (Stretchable) Images
      6. -
      -
    4. -
    5. Animation
    6. -
    7. Menus
    8. -
    9. Layout -
        -
      1. Custom Layout Resources -
      -
    10. -
    11. Styles and Themes
    12. -
    13. Searchable
    14. -
    -
    -

    This page describes the different types of resources that you can -externalize from your code and package with your application.

    +

    Each of the documents in this section describe the usage, format and syntax for a certain type +of application resource that you can provide in your resources directory ({@code res/}).

    - -

    For more details on how to use resources in your application, please see the - Resources and Internationalization - documentation.

    - - -

    Simple Values

    - -

    All simple resource values can be expressed as a string, using various -formats to unambiguously indicate the type of resource being created. For -this reason, these values can be defined both as standard resources -(under res/values/), as well as direct values supplied for -mappings in styles and themes, and attributes in -XML files such as layouts.

    - - - -

    Color Values

    -

    - A color value specifies an RGB value with an alpha channel, which can - be used in various places such as specifying a solid color for a {@link android.graphics.drawable.Drawable} - or the color to use for text. A color value always begins with - a pound (#) character and then followed by the Alpha-Red-Green-Blue information - in one of the following formats: -

    -
      -
    • #RGB -
    • #ARGB -
    • #RRGGBB -
    • #AARRGGBB -
    -

    - If you want to retrieve the color represented by a resource ID, you can call - the {@link android.content.res.Resources#getColor(int) Resources.getColor()} method. -

    -

    - Source file format: XML file requiring a - <?xml version="1.0" encoding="utf-8"?> declaration, and - a root <resources> element containing one or more - <color> tags. -

    -

    - Resource source file location: {@code res/values/colors.xml} (File name is arbitrary.) -

    -

    - Compiled resource datatype: Resource pointer to a Java int. -

    -

    - Resource reference name: -

    -
      -
    • - Java: R.color.some_name -
    • -
    • - XML: @[package:]color/some_name (where some_name is the name of a specific color) -
    • -
    -

    - Syntax -

    -
    -<color name=color_name>#color_value</color>
    -
    -
    -
    - <color> -
    -
    - Value is a color, using web-style syntax, as describe above. Has only one attribute: -
      -
    • - name - The name used in referring to this color. -
    • -
    -
    -
    -

    - Example XML Declaration -

    -

    - The following code declares two colors, the first fully opaque, and the - second translucent. -

    -
    -<resources>
    -   <color name="opaque_red">#f00</color>
    -   <color name="translucent_red">#80ff0000</color>
    -</resources>
    -
    -

    - Example Code Use -

    -

    - Example Java code -

    -
    -// Retrieve a color value.
    -int color = getResources.getColor(R.color.opaque_red);
    -
    -

    - Example XML code -

    -
    -<TextView android:layout_width="fill_parent"
    -          android:layout_height="wrap_content"
    -          android:textAlign="center"
    -          android:textColor="@color/translucent_red"
    -          android:text="Some Text"/>
    -
    - - - -

    Strings and Styled Text

    -

    - Strings, with optional simple formatting, can be -stored and retrieved as resources. You can add formatting to your string by -using three standard HTML tags: <b>, <i>, and <u>. To -guarantee getting an unstyled string only (the raw text) call the -toString() method of the retrieved CharSequence object. -Methods that accept string resources should be able to process these styling -tags. -

    -

    - If you want to retrieve the String represented by a resource ID, you can call the {@link android.content.Context#getString(int) Context.getString()} method. -

    -

    - Note: If you use an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other kind of enclosing quotes: -

    -
    -<string name="good_example">"This'll work"</string>
    -<string name="good_example_2">This\'ll also work</string>
    -<string name="bad_example">This won't work!</string>
    -<string name="bad_example_2">XML encodings won&apos;t work either!</string>
    -
    -

    - Source file format: XML file requiring a <?xml version="1.0" encoding="utf-8"?> declaration, and a root <resources> element containing one or more <string> tags. -

    -

    - Resource source file location: {@code res/values/strings.xml} (File name is arbitrary.) -

    -

    - Compiled resource datatype: Resource pointer to a Java CharSequence. -

    -

    - Resource reference name: -

    -
      -
    • - Java: R.string.some_name -
    • -
    • - XML: @[package:]string/some_name (where some_name is the name of a specific string) -
    • -
    -

    - Syntax -

    -
    -<string name=string_name>string_value</string>
    -
    -
    -
    - <string> -
    -
    - Value is a string, with optional styling tags. Has only one attribute: -
      -
    • - name - The name used in referring to this string. -
    • -
    -
    -
    -

    - Example XML Declaration -

    -

    - The following declares two strings: the first — simple text with no - formatting (resulting in a CharSequence that is simply a String object) — the second includes formatting information in the string (resulting - in a CharSequence that is a complex data structure). If you are using the custom editor for string files in Eclipse, the HTML formatting tags will automatically be escaped and you will need to use {@link android.content.Context#getString(int) Context.getString()} and {@link android.text.Html#fromHtml} to retrieve the resource and then convert it to formatted text. -

    -
    -<resources>
    -   <string name="simple_welcome_message">Welcome!</string>
    -   <string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string>
    -</resources>
    -
    -

    - Example Code Use -

    -

    - Example Java code -

    -
    -// Assign a styled string resource to a TextView
    -// on the current screen.
    -CharSequence str = getString(R.string.styled_welcome_message);
    -TextView tv = (TextView)findViewByID(R.id.text);
    -tv.setText(str);
    -
    -

    - Example XML code -

    -
    -<TextView android:layout_width="fill_parent"
    -          android:layout_height="wrap_content"
    -          android:textAlign="center"
    -          android:text="@string/simple_welcome_message"/> 
    -
    - - -

    Using Styled Text as a Format String

    -

    -Sometimes you may want to create a styled text resource that is also used as a -format string. This cannot be done directly because there is no way of passing -the styled text as the format string argument of String.format() -without stripping out the style information. The workaround is to store the -style tags as escaped HTML tags, and then convert the escaped HTML string into -a styled text after formatting has taken place. -

    -

    -To use styled text as a format string, do the following. -

    -
      -
    1. Store your styled text resource as an escaped string, so that the HTML tags in your text resource are not interpreted as if they were XML tags: -
      -<resources>
      -  <string name="search_results_resultsTextFormat">%1$d results for &lt;b>&amp;quot;%2$s&amp;quot;&lt;/b></string>
      -</resources>
      -
      -

      -In this example the format string has two arguments: %1$d is a decimal number, %2$s is a string. -

      -
    2. -
    3. - Make sure any String arguments are properly escaped if they might contain '<' or '&' characters. -The {@link android.text.TextUtils#htmlEncode} method will do this: -
      -String escapedTitle = TextUtil.htmlEncode(title);
      -
      -
    4. -
    5. - Use String.format() to format the HTML text, then use {@link android.text.Html#fromHtml} to convert the HTML text into styled text: -
      -String resultsTextFormat = getContext().getResources().getString(R.string.search_results_resultsTextFormat);
      -String resultsText = String.format(resultsTextFormat, count, escapedTitle);
      -CharSequence styledResults = Html.fromHtml(resultsText);
      -
      -
    6. -
    - - -

    -

    You can create common dimensions to use for various screen elements by -defining dimension values in XML. A dimension resource is a number followed by -a unit of measurement. For example: 10px, 2in, 5sp. Here are the units of -measurement supported by Android:

    -
    -
    px
    -
    Pixels - corresponds to actual pixels on the screen.
    - -
    in
    -
    Inches - based on the physical size of the screen.
    - -
    mm
    -
    Millimeters - based on the physical size of the screen.
    - -
    pt
    -
    Points - 1/72 of an inch based on the physical size of the screen.
    - -
    dp
    -
    Density-independent Pixels - an abstract unit that is based on the - physical density of the screen. These units are relative to a 160 dpi - screen, so one dp is one pixel on a 160 dpi screen. The ratio of - dp-to-pixel will change with the screen density, but not necessarily - in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
    - -
    sp
    -
    Scale-independent Pixels - this is like the dp unit, but it is also - scaled by the user's font size preference. It is recommend you use this - unit when specifying font sizes, so they will be adjusted for both the - screen density and user's preference.
    -
    - -

    Dimension values are not normally used as raw resources, but rather as -attribute values in XML files. You can, however, create plain resources -containing this data type.

    - -

    Source file format: XML file requiring a <?xml -version="1.0" encoding="utf-8"?> declaration, and a root -<resources> element containing one or more -<dimen> tags.

    - -

    Resource source file location: {@code res/values/dimens.xml} (File -name is arbitrary, but standard practice is to put all dimensions in one file -devoted to dimensions.)

    -

    Compiled resource datatype: Resource pointer to a -dimension.

    -

    - Resource reference name: -

    -
      -
    • - Java: R.dimen.some_name -
    • -
    • - XML: @[package:]dimen/some_name (where some_name is the name of a specific <dimen> element) -
    • -
    -

    - Syntax -

    -
    -<dimen name=dimen_name>dimen_value</dimen>
    -
    -
    -
    - <dimen> -
    -
    - A valid dimension value. -
      -
    • - name - The name used in referring to this dimension. -
    • -
    -
    -
    -

    - Example XML Declaration -

    -

    - The following code declares several dimension values. -

    -
    -<resources>
    -    <dimen name="one_pixel">1px</dimen>
    -    <dimen name="double_density">2dp</dimen>
    -    <dimen name="sixteen_sp">16sp</dimen>
    -</resources>
    -
    -

    - Example Code Use -

    -

    - Example Java code: -

    -
    -float dimen = Resources.getDimen(R.dimen.one_pixel);
    -
    -

    - Example XML code: -

    -
    -<TextView android:layout_width="fill_parent"
    -          android:layout_height="wrap_content"
    -          android:textSize="@dimen/sixteen_sp"/>
    -
    - - -

    Drawables

    - -

    A {@link android.graphics.drawable.Drawable} is a type of resource that -you retrieve with {@link android.content.res.Resources#getDrawable -Resources.getDrawable()} and use to draw to the screen. There are a -number of drawable resources that can be created.

    - - - -

    Bitmap Files

    -

    Android supports bitmap resource files in a few different formats: png -(preferred), jpg (acceptable), gif (discouraged). The bitmap file itself is -compiled and referenced by the file name without the extension (so -res/drawable/my_picture.png would be referenced as R.drawable.my_picture).

    - -

    - Source file formats: png (preferred), jpg (acceptable), gif (discouraged). One resource per file. -

    -

    - Resource file location: {@code res/drawable/some_file.png} -

    -

    - Compiled resource datatype: Resource pointer to a {@link android.graphics.drawable.BitmapDrawable BitmapDrawable}. -

    -

    - Resource reference name: -

    -
      -
    • - Java: R.drawable.some_file -
    • -
    • - XML: @[package:]drawable/some_file -
    • -
    - -

    For more discussion and examples using drawable resources, see the discussion in 2D Graphics.

    - - -

    Color Drawables

    -

    You can create a {@link android.graphics.drawable.PaintDrawable} object that is a rectangle of color, -with optionally rounded corners. This element can be defined in any of the -files inside res/values/.

    -

    Source file format: XML file requiring a <?xml -version="1.0" encoding="utf-8"?> declaration, and a root -<resources> element containing one or more -<drawable> tags.

    -

    - Resource source file location: {@code res/values/colors.xml} (File name is arbitrary, but standard practice is to put the PaintDrawable - items in the file along with the numeric color values.) -

    -

    - Compiled resource datatype: Resource pointer to a {@link android.graphics.drawable.PaintDrawable}. -

    -

    - Resource reference name: -

    -
      -
    • - Java: R.drawable.some_name -
    • -
    • - XML: @[package:]drawable/some_name (where some_name is the name of a specific resource) -
    • -
    -

    - Syntax -

    -
    -<drawable name=color_name>color_value</drawable>
    -
    -
    -
    - <drawable> -
    -
    - A valid color value. -
      -
    • - name - The name used in referring to this drawable. -
    • -
    -
    -
    -

    - Example XML Declaration -

    -

    - The following code declares several color drawables. -

    -
    -<resources>
    -    <drawable name="solid_red">#f00</drawable>
    -    <drawable name="solid_blue">#0000ff</drawable>
    -    <drawable name="solid_green">#f0f0</drawable>
    -</resources>
    -
    -

    - Example Code Use -

    -

    - Example Java code -

    -
    -// Assign a PaintDrawable as the background to
    -// a TextView on the current screen.
    -Drawable redDrawable = Resources.getDrawable(R.drawable.solid_red);
    -TextView tv = (TextView)findViewByID(R.id.text);
    -tv.setBackground(redDrawable);
    -
    -

    - Example XML code -

    -
    -<TextView android:layout_width="fill_parent"
    -          android:layout_height="wrap_content"
    -          android:textAlign="center"
    -          android:background="@drawable/solid_red"/>
    -
    - - -

    Nine-Patch (stretchable) Images

    -

    - Android supports a stretchable bitmap image, called a - {@link android.graphics.NinePatch} graphic. This is a PNG image in which - you define stretchable sections that Android will resize to fit the object - at display time to accommodate variable sized sections, such as text strings. - You typically assign this resource to the View's background. An example use - of a stretchable image is the button backgrounds that Android uses; buttons - must stretch to accommodate strings of various lengths. -

    - -

    - Source file format: PNG — one resource per file -

    -

    - Resource source file location: {@code res/drawable/some_name.9.png} (Filename must end in {@code .9.png}) -

    -

    - Compiled resource datatype: Resource pointer to a {@link android.graphics.drawable.NinePatchDrawable NinePatchDrawable}. -

    -

    - Resource reference name: -

    -
      -
    • - Java: R.drawable.some_file -
    • -
    • - XML: @[package:]drawable.some_file -
    • -
    - - -

    For more information and examples using NinePatch drawables, see the discussion -in 2D Graphics.

    - - - -

    Animation

    - -

    Tweened Animation

    -

    - Android can perform simple animation on a graphic, or a series of graphics. These include rotations, fading, moving, and stretching. -

    -

    - Source file format: XML file, one resource per file, one root tag with no <?xml> declaration -

    -

    - Resource file location: {@code res/anim/some_file.xml} -

    -

    - Compiled resource datatype: Resource pointer to an {@link android.view.animation.Animation}. -

    -

    - Resource reference name: -

    -
      -
    • - Java: R.anim.some_file -
    • -
    • - XML: @[package:]anim/some_file -
    • -
    -

    - Syntax -

    -

    - The file must have a single root element: this will be either a single <alpha>, <scale>, <translate>, <rotate>, interpolator element, or <set> element that holds groups of these elements (which may include another <set>). By default, all elements are applied simultaneously. To have them occur sequentially, you must specify the startOffset attribute. -

    -
    -<set android:shareInterpolator=boolean>  // Only required if multiple tags are used.
    -   <alpha android:fromAlpha=float
    -          android:toAlpha=float >   |
    -   <scale android:fromXScale=float
    -          android:toXScale=float
    -          android:fromYScale=float
    -          android:toYScale=float
    -          android:pivotX=string
    -          android:pivotY=string >    |
    -   <translate android:fromX=string
    -              android:toX=string
    -              android:fromY=string
    -              android:toY=string >   |
    -   <rotate android:fromDegrees=float
    -           android:toDegrees=float
    -           android:pivotX=string
    -           android:pivotY=string > |
    -   <interpolator tag>
    -   <set>
    -</set>
    -
    -

    - Elements and Attributes -

    -
    -
    - <set> -
    -
    - A container that can recursively hold itself or other animations. - Represents an {@link android.view.animation.AnimationSet}. - You can include as many child elements of the same or different types as you like. - Supports the following attribute: -
      -
    • - shareInterpolator - Whether to share the same Interpolator among all immediate child elements. -
    • -
    -
    -
    - <alpha> -
    -
    - A fading animation. Represents an {@link android.view.animation.AlphaAnimation}. - Supports the following attributes: -
      -
    • - fromAlpha - 0.0 to 1.0, where 0.0 is transparent. -
    • -
    • - toAlpha - 0.0 to 1.0, where 0.0 is transparent. -
    • -
    -
    -
    - <scale> -
    -
    - A resizing animation. Represents a {@link android.view.animation.ScaleAnimation}. - You can specify what is the center point of the image (the pinned center), from which it grows outward (or inward), by specifying pivotX and pivotY. So, for example, if these were 0, 0 (top left corner), all growth would be down and to the right. scale supports the following attributes: -
      -
    • - fromXScale - Starting X size, where 1.0 is no change. -
    • -
    • - toXScale - Ending X size, where 1.0 is no change. -
    • -
    • - fromYScale - Starting Y size, where 1.0 is no change. -
    • -
    • - toYScale - Ending Y size, where 1.0 is no change. -
    • -
    • - pivotX - The X coordinate of the pinned center. -
    • -
    • - pivotY - The Y coordinate of the pinned center. -
    • -
    -
    -
    - <translate> -
    -
    - A vertical/horizontal motion animation. - Represents a {@link android.view.animation.TranslateAnimation}. -Supports the following attributes in any of the following three formats: values from -100 to 100, ending with "%", indicating a percentage relative to itself; values from -100 to 100, ending in "%p", indicating a percentage relative to its parent; a float with no suffix, indicating an absolute value. -
      -
    • - fromXDelta - Starting X location. -
    • -
    • - toXDelta - Ending X location. -
    • -
    • - fromYDelta - Starting Y location. -
    • -
    • - toYDelta - Ending Y location. -
    • -
    -
    -
    - <rotate> -
    -
    - A rotation animation. Represents a {@link android.view.animation.RotateAnimation}. - Supports the following attributes: -
      -
    • - fromDegrees - Starting rotation, in degrees. -
    • -
    • - toDegrees - Ending rotation, in degrees. -
    • -
    • - pivotX - The X coordinate of the center of rotation, in pixels, where (0,0) is the top left corner. -
    • -
    • - pivotY - The Y coordinate of the center of rotation, in pixels, where (0,0) is the top left corner. -
    • -
    -
    -
    - <interpolator tag> -
    -
    - You can also use any of the interpolator subclass elements defined in {@link android.R.styleable}. Examples include <CycleInterpolator>, <EaseInInterpolator>, and <EaseOutInterpolator>. These objects define a velocity curve that describes how quickly a visual action takes place on a timeline (fast at first and slow later, slow at first and gradually faster, and so on). -
    -
    -

    -In addition to the attributes defined for each element above, the elements -<alpha>, <scale>, <translate>, -<rotate>, and <set> all support the following attributes (inherited -from the {@link android.view.animation.Animation} class): -

    -
    -
    {@link android.R.attr#duration duration}
    -
    - Duration, in milliseconds, for this effect. -
    -
    {@link android.R.attr#startOffset startOffset}
    -
    - Offset start time for this effect, in milliseconds. -
    -
    {@link android.R.attr#fillBefore fillBefore}
    -
    - When set true, the animation transformation is applied before the animation begins. -
    -
    {@link android.R.attr#fillAfter fillAfter}
    -
    - When set true, the animation transformation is applied after the animation ends. -
    -
    {@link android.R.attr#repeatCount repeatCount}
    -
    - Defines the number of times the animation should repeat. -
    -
    {@link android.R.attr#repeatMode repeatMode}
    -
    - Defines the animation behavior when it reaches the end and the repeat count is greater than 0. - Options are to either restart or reverse the animation. -
    -
    {@link android.R.attr#zAdjustment zAdjustment}
    -
    - Defines the z-axis ordering mode to use when running the animation (normal, top, or bottom). -
    -
    {@link android.R.attr#interpolator interpolator}
    -
    - You can optionally set an interpolator for each element to determine how quickly or slowly it performs its effect over time. For example, slow at the beginning and faster at the end for EaseInInterpolator, and the reverse for EaseOutInterpolator. A list of interpolators is given in {@link android.R.anim}. To specify these, use the syntax @android:anim/interpolatorName. -
    -
    - -

    For more discussion and animation code samples, see the discussion in the -2D Graphics document.

    - - - - -

    Application menus (Options Menu, Context Menu, or Sub Menu) can be defined as -XML resources and inflated by your application using {@link android.view.MenuInflater}.

    - -

    Source file format: XML file, one resource per file, one root tag, -<?xml> declaration not required.

    -

    Resource file location: res/menu/some_file.xml

    -

    Compiled resource datatype: Resource pointer to a {@link android.view.Menu} (or subclass) resource.

    -

    Resource reference name:

    -
    • Java: R.menu.some_file
    - -

    Syntax

    -

    The file must have a single root element: a <menu> element. In all, -there are three valid elements: <menu>, <group> and <item>. The -<item> and <group> elements must be the children of a <menu>, but <item> -elements can also be the children of a <group>, and another <menu> element may be the child -of an <item> (to create a Sub Menu).

    -
    -<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/example_item2
    -              android:title="Example Item 2"
    -              android:icon="@drawable/example_item2_icon" />
    -    </group>
    -
    -    <item android:id="@+id/example_submenu
    -          android:title="Example Sub Menu" >
    -        <menu>
    -            <item android:id="@+id/example_submenu_item
    -                  android:title="Example Sub Menu Item" />
    -        </menu>
    -    </item>
    -
    -</menu>
    -
    - -

    Elements and Attributes

    -

    All attributes must be defined with the android namespace (e.g., android:icon="@drawable/icon").

    -
    -
    <menu>
    -
    The root of a menu. Contains <item> and <group> nodes. No attributes.
    -
    <group>
    -
    A menu group. Contains <item> elements. Valid attributes: -
      -
    • id - A unique integer ID for the group.
    • -
    • menuCategory - Value corresponding to Menu CATEGORY_* constants — defines the priority of the group. Valid values: - container, system, secondary, and alternative.
    • -
    • orderInCategory - An integer that defines the default order of the items within the category.
    • -
    • checkableBehavior - Whether the items are checkable. Valid values: - none, all (exclusive / radio buttons), single (non-exclusive / checkboxes)
    • -
    • visible - Whether the group is visible. true or false.
    • -
    • enabled - Whether the group is enabled. true or false.
    • -
    -
    -
    <item>
    -
    A menu item. May contain a <menu> element (for a Sub Menu). Valid attributes: -
      -
    • id - A unique resource ID for the item.
    • -
    • menuCategory - Used to define the menu category.
    • -
    • orderInCategory - Used to define the order of the item, within a group.
    • -
    • title - A string for the menu title.
    • -
    • titleCondensed - A condensed string title, for situations in which the normal title is too long.
    • -
    • icon - A resource identifier for a drawable icon.
    • -
    • alphabeticShortcut - A character for the alphabetic shortcut key.
    • -
    • numericShortcut - A number for the numeric shortcut key.
    • -
    • checkable - Whether the item is checkable. true or false.
    • -
    • checked - Whether the item is checked by default. true or false.
    • -
    • visible - Whether the item is visible by default. true or false.
    • -
    • enabled - Whether the item is enabled by default. true or false.
    • -
    -
    -
    - -

    For more discussion on how to create menus in XML and inflate them in your application, -read Creating Menus.

    - - - -

    Layout

    -

    Android lets you specify screen layouts using XML elements inside an XML -file, similar to designing screen layout for a webpage in an HTML file. Each -file contains a whole screen or a part of a screen, and is compiled into a -View resource that can be passed in to -{@link android.app.Activity#setContentView(int) Activity.setContentView} or used as a -reference by other layout resource elements. Files are saved in the -res/layout/ folder of your project, and compiled by the Android resource -compiler, aapt.

    - -

    Every layout XML file must evaluate to a single root -element. First we'll describe how to use the standard XML tags understood by -Android as it is shipped, and then we'll give a little information on how you -can define your own custom XML elements for custom View objects. - -

    The root element must have -the Android namespace "http://schemas.android.com/apk/res/android" defined in -the root element.

    - -

    For a complete discussion on creating layouts, see the -User Interface topic.

    - -

    Source file format: XML file -requiring a <?xml version="1.0" encoding="utf-8"?> -declaration, and a root element of one of the supported XML layout elements. -

    - - -

    Resource file location: -res/layout/some_file.xml.

    -

    - Compiled resource datatype: Resource pointer to a {@link android.view.View} (or subclass) resource. -

    -

    - Resource reference name: -

    -
      -
    • - Java: R.layout.some_file -
    • -
    • - XML: @[package:]layout/some_file -
    • -
    -

    - Syntax -

    -
    -<ViewGroupClass xmlns:android="http://schemas.android.com/apk/res/android"
    -                android:id="@+id/string_name" (attributes)>
    -   <widget or other nested ViewGroupClass>+
    -   <requestFocus/>(0 or 1 per layout file, assigned to any element)
    -</ViewGroupClass>
    -
    -
    -
    - <ViewGroupClass> -
    -
    -

    The file must have a single root element. This can be a ViewGroup class that contains other elements, or a widget (or custom item) if it's only one object. By default, you can use any (case-sensitive) Android {@link android.widget widget} or {@link android.view.ViewGroup ViewGroup} class name as an element. These elements support attributes that apply to the underlying class, but the naming is not as clear. How to discover what attributes are supported for what tags is discussed below. You should not assume that any nesting is valid (for example you cannot enclose <TextView> elements inside a <ListLayout>).

    -

    If a class derives from another class, the XML element inherits all the attributes from the element that it "derives" from. So, for example, <EditText> is the corresponding XML element for the EditText class. It exposes its own unique attributes (EditText_numeric), as well as all attributes supported by <TextView> and <View>. For the id attribute of a tag in XML, you should use a special syntax: "@+id/somestringvalue". The "@+" syntax creates a resource number in the R.id class, if one doesn't exist, or uses it, if it does exist. When declaring an ID value for an XML tag, use this syntax. Example: <TextView android:id="@+id/nameTextbox"/>, and refer to it this way in Java: findViewById(R.id.nameTextbox). All elements support the following values:

    -
      -
    • - id - An ID value used to access this element in Java. Typically you will use the syntax @+id/string_name to generate an ID for you in the id.xml file if you haven't created one yourself. -
    • -
    • - xmlns:android="http://schemas.android.com/apk/res/android" - Required for the root element only. -
    • -
    -
    -
    - <requestFocus> -
    -
    - Any element representing a View object can include this empty element, which gives it's parent tag initial focus on the screen. You can have only one of these elements per file. -
    -
    -

    - What Attributes Are Supported for What Elements? -

    -

    - Android uses the {@link android.view.LayoutInflater} class at run time to load an XML layout resource and translate it into visual elements. By default, all widget class names are supported directly as tags, but a full list of supported tags and attributes is listed in the {@link android.R.styleable} reference page. However, the attribute names are somewhat obscure. If an underscore appears in the name, this indicates that it is an attribute — typically of the element before the underscore. So, for example, EditText_autoText means that the <EditText> tag supports an attribute autoText. When you actually use the attribute in that element, use only the portion after the last underscore, and prefix the attribute with the prefix "android:". So, for example, if {@link android.R.styleable} lists the following values: -

    -
      -
    • - TextView -
    • -
    • - TextView_lines -
    • -
    • - TextView_maxlines -
    • -
    -

    - You could create an element like this: -

    -
    -<TextView android:lines="10" android:maxlines="20"/>
    -
    -

    - This would create a {@link android.widget.TextView} object and set its lines and maxlines properties. -

    -

    - Attributes come from three sources: -

    -
      -
    • - Attributes exposed directly by the element. For example, TextView supports TextView_text, as discussed above. -
    • -
    • - Attributes exposed by all the superclasses of that element. For example, the TextView class extends the View class, so the <TextView> element supports all the attributes that the <View> element exposes — a long list, including View_paddingBottom and View_scrollbars. These too are used without the class name: <TextView android:paddingBottom="20" android:scrollbars="horizontal" />. -
    • -
    • - Attributes of the object's {@link android.view.ViewGroup.LayoutParams} subclass. All View objects support a LayoutParams member (see Declaring Layout). To set properties on an element's LayoutParams member, the attribute to use is "android:layout_layoutParamsProperty". For example: android:layout_gravity for an object wrapped by a <LinearLayout> element. Remember that each LayoutParams subclass also supports inherited attributes. Attributes exposed by each subclass are given in the format someLayoutParamsSubclass_Layout_layout_someproperty. This defines an attribute "android:layout_someproperty". Here is an example of how Android documentation lists the properties of the {@link android.widget.LinearLayout.LayoutParams LinearLayout.LayoutParams} class: -
    • -
    -
      -
    • LinearLayout_Layout // The actual object — not used. -
    • -
    • LinearLayout_Layout_layout_gravity // Exposes a gravity attribute -
    • -
    • LinearLayout_Layout_layout_height // Exposes a height attribute -
    • -
    • LinearLayout_Layout_layout_weight // Exposes a weight attribute -
    • -
    • LinearLayout_Layout_layout_width // Exposes a width attribute -
    • -
    -

    - Here is an example that sets some of these values on a few objects, including direct attributes, inherited attributes, and LayoutParams attributes: -

    -
    -<?xml version="1.0" encoding="utf-8"?>
    -<!-- res/main_screen.xml -->
    -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    -              android:orientation="vertical"    // The object's own orientation property
    -              android:padding="4"               // Inherited View property
    -              android:gravity="center"          // The object's own property
    -              android:layout_width="fill_parent"  // Parent object's LinearLayout.LayoutParams.width
    -              android:layout_height="fill_parent"> // Parent object's LinearLayout.LayoutParams.height
    -
    -   <TextView android:layout_width="fill_parent"   // TextView.LayoutParams.width
    -             android:layout_height="wrap_content" // TextView.LayoutParams.height
    -             android:layout_weight="0"            // TextView.LayoutParams.weight
    -             android:paddingBottom="4"            // TextView.paddingBottom
    -             android:text="@string/redirect_getter"/> // TextView.text
    -
    -   <EditText android:id="@+id/text"
    -             android:layout_width="fill_parent"   // EditText.LayoutParams.width
    -             android:layout_height="wrap_content" // EditText.LayoutParams.height
    -             android:layout_weight="0"            // EditText.LinearLayoutParams.weight
    -             android:paddingBottom="4">           // EditText.paddingBottom
    -       <requestFocus />
    -   </EditText>
    -
    -   <Button android:id="@+id/apply"
    -           android:layout_width="wrap_content"  // Button.LayoutParams.width
    -           android:layout_height="wrap_content" // Button.LayoutParams.height
    -           android:text="@string/apply" />      // TextView.text
    -</LinearLayout>
    -
    -

    - Example Code Use -

    -

    - The most common use is to load the XML file (located at res/main_screen.xml) and use it as the current screen, as shown here with the preceding file: -

    -
    -setContentView(R.layout.main_screen); 
    -
    -

    - However, layout elements can also represent repeating elements used as templates. -

    - -

    Also see User Interface for more information on layouts.

    - - - -

    Custom Layout Resources

    -

    - You can define custom elements to use in layout resources. These custom elements can then be used the same as any Android layout elements: that is, you can use them and specify their attributes in other resources. The ApiDemos sample application has an example of creating a custom layout XML tag, LabelView. To create a custom element, you will need the following files: -

    -
      -
    • - Java implementation file - The implementation file. The class must extend {@link android.view.View View} or a subclass. See LabelView.java in ApiDemos. -
    • -
    • - res/values/attrs.xml - Defines the XML element, and the attributes that it supports, for clients to use to instantiate your object in their layout XML file. Define your element in a <declare-styleable name=your_java_class_name>. See res/values/attrs.xml in ApiDemos. -
    • -
    • - res/layout/your_class.xml [optional] - An optional XML file to describe the layout of your object. This could also be done in Java. See custom_view_1.xml in ApiDemos. -
    • -
    -

    - Source file format: XML file without an <?xml> declaration, and a <resources> root element containing one or more custom element tags. -

    -

    - Resource file location: {@code res/values/attrs.xml} (File name is arbitrary.) -

    -

    - Compiled resource datatype: Resource pointer to a {@link android.view.View} (or subclass) resource. -

    -

    - Resource reference name: R.styleable.some_file (Java). -

    - - - -

    Styles and Themes

    -

    - A style is one or more attributes applied to a single element (for example, 10 point red Arial font, applied to a TextView). A style is applied as an attribute to an element in a layout XML file. -

    -

    - A theme is one or more attributes applied to a whole screen — for example, you might apply the stock Android Theme.dialog theme to an activity designed to be a floating dialog box. A theme is assigned as an attribute to an Activity in the manifest file. -

    -

    - Both styles and themes are defined in a <style> block containing one or more string or numerical values (typically color values), or references to other resources (drawables and so on). These elements support inheritance, so you could have MyBaseTheme, MyBaseTheme.Fancy, MyBaseTheme.Small, and so on. -

    - -

    For a complete discussion on styles and themes, read -Applying Styles and Themes.

    - -

    - Source file format: XML file requiring a <?xml version="1.0" encoding="utf-8"?> declaration, and a root <resources> element containing one or more <style> tags. -

    -

    - Resource source file location: {@code res/values/styles.xml} (File name is arbitrary, but standard practice is to put all styles into a file named styles.xml.) -

    -

    - Compiled resource datatype: Resource pointer to a Java CharSequence. -

    -

    - Resource reference name: -

    -
      -
    • - Java: R.style.styleID for the whole style, R.style.styleID.itemID for an individual setting -
    • -
    • - XML: @[package:]style/styleID for a whole style, @[package:]style/styleID/itemID for an individual item. Note: to refer to a value in the currently applied theme, use "?" instead of "@" as described below (XML). -
    • -
    -

    - Syntax -

    -
    -<style name=string [parent=string] >
    -   <item name=string>Hex value | string value | reference</item>+
    -</style>
    -
    -
    -
    - <style> -
    -
    - Holds one or more <item> elements, each describing one value. This style, which is a bundle of values, can be referred to as a theme. -
      -
    • - name - The name used in referring to this theme. -
    • -
    • - parent - An optional parent theme. All values from the specified theme will be inherited into this theme. Any values with identical names that you specify will override inherited values. The name must be qualified by the package, but you don't need the /style directive (for example, android:Theme for the base Android theme, or MyTheme for a theme defined in your package). -
    • -
    -
    -
    - <item> -
    -
    - A value to use in this theme. It can be a standard string, a hex color value, or a reference to any other resource type. -
    -
    - -

    For examples of how to declare and apply styles and themes, read -Applying Styles and Themes.

    - - - -

    Searchable

    - -

    To make search appear to the user as a seamless system-wide feature, the Android framework -offers APIs that let applications control how they are searched. -Applications can customize how search is invoked, how the search dialog looks, and what type of -search results are available, including suggestions that are shown as the user types.

    - -

    In order to utilize the Android search framework, an application must provide a search configuration -in the form of an XML resource. -This section describes the search configuration XML in terms of its syntax and usage. For a more -complete discussion about how to implement search features for your application, see - -{@link android.app.SearchManager}.

    - -

    - Source file format: - XML file requiring a <?xml version="1.0" encoding="utf-8"?> - declaration, and a root <searchable> element. -

    - -

    - Resource source file location: {@code res/xml/searchable.xml} - (The file name is arbitrary, but standard practice is to use searchable.xml.) -

    - -

    - Compiled resource datatype: - Resource pointer to an xml object. -

    - -

    - Resource reference name: -

    - -
      -
    • - Java: - R.xml.filename. -
    • -
    • - XML: - @[package:]xml/filename (e.g., @xml/searchable). -
    • -
    - -

    - Syntax -

    - -
    -<searchable xmlns:android="http://schemas.android.com/apk/res/android
    -     android:label="@string/search_label" 
    -     ... >
    -     <actionkey
    -          android:keycode="KEYCODE_CALL"
    -          ... >     
    -</searchable>
    -
    +

    Here's a brief summary of each resource type:

    -
    - <searchable> -
    -
    - Defines all application search configurations, including settings for text and voice searches - performed within the application. It accepts the following attributes: -
      -
    • - label - Required. This is the name for your application, as it - will appear to the user. This will be visible only if searchMode is set to - "showSearchLabelAsBadge" (see below). -
    • -
    • - hint - This is the text to display in the search text field when no text has - been entered. This is recommended in order to provide context to the search about to be - performed (e.g., "Search the Dictionary"). -
    • -
    • - searchMode - If provided and non-zero, this sets additional modes for control - of the search presentation. The following mode values are accepted: -
        -
      • - showSearchLabelAsBadge - If set, this enables the display of the - search target (label) within the search bar. -
      • -
      • - queryRewriteFromData - If set, this causes the suggestion column - SUGGEST_COLUMN_INTENT_DATA to be considered as the text for suggestion query - rewriting. This should only be used when the values in - SUGGEST_COLUMN_INTENT_DATA are suitable for user inspection and editing - - typically, HTTP/HTTPS Uri's. -
      • -
      • - queryRewriteFromText - If set, this causes the suggestion - column SUGGEST_COLUMN_TEXT_1 to be considered as the text for suggestion query - rewriting. This should be used for suggestions in which no query - text is provided and the SUGGEST_COLUMN_INTENT_DATA values are not suitable - for user inspection and editing. -
      • -
      -

      More than one of the above values for searchMode can be used at once. For - example, you can declare two modes at once, like this: - searchMode="queryRewriteFromData|queryRewriteFromText" -

    • -
    • - inputType - If provided, supplies a hint about the type of search text - the user will be entering. For most searches, in which free form text is expected, - this attribute is not needed. See - {@link android.R.attr#inputType} for a list of suitable values for this attribute. -
    • -
    • - imeOptions - If provided, supplies additional options for the input method. - For most searches, in which free form text is expected, this attribute is not needed, - and will default to "actionSearch". See - {@link android.R.attr#imeOptions} for a list of suitable values for this attribute. -
    • -
    - -

    If you have defined a content provider to generate search suggestions, you need to - provide some more searchable metadata in order to configure communications with the content - provider. The following are additional {@code <searchable>} attributes for use when - providing search suggestions:

    - -
      -
    • - searchSuggestAuthority - Required to provide search suggestions. - This value must match the authority string provided in the provider section of your manifest. -
    • -
    • - searchSuggestPath - If provided, this path will be inserted in the suggestions - query Uri, after the authority you have provide but before the standard suggestions path. - This is only required if you have a single content provider issuing different types - of suggestions (e.g. for different data types) and you need - a way to disambiguate the suggestions queries when they are received. -
    • -
    • - searchSuggestSelection - If provided, this value will be passed into your - query function as the selection parameter. Typically this will be a WHERE clause for - your database, and will contain a single question mark, which represents the actual query - string that has been typed by the user. However, you can also use any non-null value to simply - trigger the delivery of the query text (via selection arguments), and then use the query - text in any way appropriate for your provider (ignoring the actual text of the selection parameter.) -
    • -
    • - searchSuggestIntentAction - The default Intent action to be used when a user - clicks on a search suggestion. - If provided, and not overridden by the selected suggestion, this value will - be placed in the action field of the {@link android.content.Intent} when the - user clicks a suggestion. -
    • -
    • - searchSuggestIntentData - The default Intent data to be used when a user - clicks on a search suggestion. - If provided, and not overridden by the selected suggestion, this value will be - placed in the data field of the {@link android.content.Intent} when the user clicks - a suggestion. -
    • -
    - -

    Beyond providing search suggestions while using your application's local search, you - can also configure your search suggestions to be made available to Quick Search Box, - which will allow users so receive search suggestions from your application content from outside - your application. The following are additional {@code <searchable>} attributes for use when - providing search suggestions to Quick Search Box:

    - -
      -
    • - includeInGlobalSearch - Required to provide search suggestions in - Quick Search Box. If true, this indicates the search suggestions provided by your - application should be included in the globally accessible Quick Search Box. The user must - still enable your application as a searchable item in the system search settings in order - for your suggestions to appear in Quick Search Box. -
    • -
    • - searchSettingsDescription - If provided, this provides a brief description - of the search suggestions that you provide to Quick Search Box, - and will be displayed in the search settings entry for your application. -
    • -
    • - queryAfterZeroResults - Indicates whether a source should be invoked for - supersets of queries it has returned zero results for in the past. For example, if a - source returned zero results for "bo", it would be ignored for "bob". If set to false, - this source will only be ignored for a single session; the next time the search dialog - is invoked, all sources will be queried. The default value is false. -
    • -
    • - searchSuggestThreshold - Indicates the minimum number of characters needed to - trigger a source lookup from Quick Search Box. Only guarantees that a source will not be - queried for anything shorter than the threshold. The default value is 0. -
    • -
    - -

    To enable voice search for your Activity, you can add fields to the searchable metadata - that enable and configure voice search. The following are additional {@code <searchable>} - attributes for use when implementing voice search:

    - -
      -
    • - voiceSearchMode - Required to provide voice search - capabilities. - If provided and non-zero, enables voice search. - (Voice search may not be provided by the device, in which case these flags will - have no effect.) The following mode values are accepted: -
        -
      • - showVoiceSearchButton - If set, display a voice search button. This only - takes effect if voice search is available on the device. If set, then "launchWebSearch" - or "launchRecognizer" must also be set. -
      • -
      • - launchWebSearch - If set, the voice search button will take the user directly - to a built-in voice web search activity. Most applications will not use this flag, as - it will take the user away from the activity in which search was invoked. -
      • -
      • - launchRecognizer - If set, the voice search button will take - the user directly to a built-in voice recording activity. This activity - will prompt the user to speak, transcribe the spoken text, and forward the resulting - query text to the searchable activity, just as if the user had typed it into the - search UI and clicked the search button. -
      • -
      -
    • -
    • - voiceLanguageModel - A string constant from - {@link android.speech.RecognizerIntent}. - If provided, this specifies the language model that - should be used by the voice recognition system. See - {@link android.speech.RecognizerIntent#EXTRA_LANGUAGE_MODEL } for more - information. If not provided, the default value - {@link android.speech.RecognizerIntent#LANGUAGE_MODEL_FREE_FORM } will be used. -
    • -
    • - voicePromptText - A string. If provided, this specifies a prompt - that will be displayed during voice input. If not provided, a default prompt - will be displayed. -
    • -
    • - voiceLanguage - A string constant from {@link java.util.Locale}. - If provided, this specifies the spoken language to be expected. - This is only needed if it is different from the current value of - {@link java.util.Locale#getDefault()}. -
    • -
    • - voiceMaxResults - If provided, enforces the maximum number of results to return, - including the "best" result which will always be provided as the SEARCH intent's primary - query. Must be one or greater. Use EXTRA_RESULTS to get the results from the intent. - If not provided, the recognizer will choose how many results to return. -
    • -
    -
    - -
    - <actionkey> -
    -
    - Defines a shortcut key for a search action. -
      -
    • - keycode - Required. This attribute denotes the action key - you wish to respond to. Note that not all action keys are actually supported using - this mechanism, as many of them are used for typing, - navigation, or system functions. This will be added to the - {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} Intent that is passed to your - searchable Activity. To examine the key code, use - {@link android.content.Intent#getIntExtra getIntExtra(SearchManager.ACTION_KEY)}. - Note that, in addition to the keycode, you must also provide one or more of - the action specifier attributes below. -
    • -
    • - queryActionMsg - If you wish to handle an action key during normal - search query entry, you must define an action string here. This will be added to the - {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} Intent that is - passed to your searchable Activity. To examine the string, use - {@link android.content.Intent#getStringExtra - getStringExtra(SearchManager.ACTION_MSG)}. -
    • -
    • - suggestActionMsg - If you wish to handle an action key while a - suggestion is being displayed and selected, there are two ways to handle this. - If all of your suggestions can handle the action key, you can simply define - the action message using this attribute. This will be added to the - {@link android.content.Intent#ACTION_SEARCH} Intent that is passed to your - searchable Activity. To examine the string, - use {@link android.content.Intent#getStringExtra - getStringExtra(SearchManager.ACTION_MSG)}. -
    • -
    • - suggestActionMsgColumn - If you wish to handle an action key while - a suggestion is being displayed and selected, but you do not wish to enable - this action key for every suggestion, then you can use this attribute to control - it on a suggestion-by-suggestion basis. First, you must define a column - (and name it here) where your suggestions will include the action string. Then, - in your content provider, you must provide this column, and when desired, - provide data in this column. The search manager will look at your suggestion cursor, - using the string provided here in order to select a column, and will use - that to select a string from the cursor. That string will be added to the - {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} - Intent that is passed to your searchable Activity. To examine - the string, use {@link android.content.Intent#getStringExtra - getStringExtra(SearchManager.ACTION_MSG)}. If the data does not exist for the - selection suggestion, the action key will be ignored. -
    • -
    -
    +
    Animation Resources
    +
    Define pre-determined animations.
    +Tween animations are saved in {@code res/anim/} and accessed from the {@code R.anim} class.
    +Frame animations are saved in {@code res/drawable/} and accessed from the {@code R.drawable} class.
    +
    Color State List Resource
    +
    Define a color resources that changes based on the View state.
    +Saved in {@code res/color/} and accessed from the {@code R.color} class.
    +
    Drawable Resources
    +
    Define various graphics with bitmaps or XML.
    +Saved in {@code res/drawable/} and accessed from the {@code R.drawable} class.
    +
    Layout Resource
    +
    Define the layout for your application UI.
    +Saved in {@code res/layout/} and accessed from the {@code R.layout} class.
    +
    Menu Resource
    +
    Define the contents of your application menus.
    +Saved in {@code res/menu/} and accessed from the {@code R.menu} class.
    +
    String Resources
    +
    Define strings, string arrays, and plurals (and include string formatting and styling).
    +Saved in {@code res/values/} and accessed from the {@code R.string}, {@code R.array}, +and {@code R.plurals} classes.
    +
    Style Resource
    +
    Define the look and format for UI elements.
    +Saved in {@code res/values/} and accessed from the {@code R.style} class.
    +
    More Resource Types
    +
    Define values such as booleans, integers, dimensions, colors, and other arrays.
    +Saved in {@code res/values/} but each accessed from unique {@code R} sub-classes (such as {@code +R.bool}, {@code R.integer}, {@code R.dimen}, etc.).
    + + + + + diff --git a/docs/html/guide/topics/resources/color-list-resource.jd b/docs/html/guide/topics/resources/color-list-resource.jd new file mode 100644 index 0000000000000..449b66f55b11d --- /dev/null +++ b/docs/html/guide/topics/resources/color-list-resource.jd @@ -0,0 +1,165 @@ +page.title=Color State List Resource +parent.title=Resource Types +parent.link=available-resources.html +@jd:body + +
    +
    +

    See also

    +
      +
    1. Color (simple value)
    2. +
    +
    +
    + + +

    A {@link android.content.res.ColorStateList} is an object you can define in XML +that you can apply as a color, but will actually change colors, depending on the state of +the {@link android.view.View} object to which it is applied. For example, a {@link +android.widget.Button} widget can exist in one of several different states (pressed, focused, +or niether) and, using a color state list, you can provide a different color during each state.

    + +

    You can describe the state list in an XML file. Each color is defined in an {@code +<item>} element inside a single {@code <selector>} element. Each {@code <item>} +uses various attributes to describe the state in which it should be used.

    + +

    During each state change, the state list is traversed top to bottom and the first item that +matches the current state will be used—the selection is not based on the "best +match," but simply the first item that meets the minimum criteria of the state.

    + +

    Note: If you want to provide a static color resource, use a +simple Color value.

    + +
    + +
    file location:
    +
    res/color/filename.xml
    +The filename will be used as the resource ID.
    + +
    compiled resource datatype:
    +
    Resource pointer to a {@link android.content.res.ColorStateList}.
    + +
    resource reference:
    +
    +In Java: R.color.filename
    +In XML: @[package:]color/filename +
    + +
    syntax:
    +
    +
    +<?xml version="1.0" encoding="utf-8"?>
    +<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    +    <item
    +        android:color="hex_color"
    +        android:state_pressed=["true" | "false"]
    +        android:state_focused=["true" | "false"]
    +        android:state_selected=["true" | "false"]
    +        android:state_active=["true" | "false"]
    +        android:state_checkable=["true" | "false"]
    +        android:state_checked=["true" | "false"]
    +        android:state_enabled=["true" | "false"]
    +        android:state_window_focused=["true" | "false"] />
    +</selector>
    +
    +
    + +
    elements:
    +
    +
    + +
    <selector>
    +
    Required. This must be the root element. Contains one or more {@code +<item>} elements. +

    attributes:

    +
    +
    xmlns:android
    +
    String. Required. Defines the XML namespace, which must be + "http://schemas.android.com/apk/res/android". +
    +
    +
    <item>
    +
    Defines a color to use during certain states, as described by its attributes. Must be a +child of a <selector> element. +

    attributes:

    +
    +
    android:color
    +
    Hexadeximal color. Required. The color is specified with an +RGB value and optional alpha channel. +

    The value always begins with a pound (#) character and then followed by the +Alpha-Red-Green-Blue information in one of the following formats:

    +
      +
    • #RGB
    • +
    • #ARGB
    • +
    • #RRGGBB
    • +
    • #AARRGGBB
    • +
    +
    android:state_pressed
    +
    Boolean. "true" if this item should be used when the object is pressed (such as when a button +is touched/clicked); "false" if this item should be used in the default, non-pressed state.
    +
    android:state_focused
    +
    Boolean. "true" if this item should be used when the object is focused (such as when a button +is highlighted using the trackball/d-pad); "false" if this item should be used in the default, +non-focused state.
    +
    android:state_selected
    +
    Boolean. "true" if this item should be used when the object is selected (such as when a +tab is opened); "false" if this item should be used when the object is not selected.
    +
    android:state_checkable
    +
    Boolean. "true" if this item should be used when the object is checkable; "false" if this +item should be used when the object is not checkable. (Only useful if the object can +transition between a checkable and non-checkable widget.)
    +
    android:state_checked
    +
    Boolean. "true" if this item should be used when the object is checked; "false" if it +should be used when the object is un-checked.
    +
    android:state_enabled
    +
    Boolean. "true" if this item should be used when the object is enabled (capable of +receiving touch/click events); "false" if it should be used when the object is disabled.
    +
    android:state_window_focused
    +
    Boolean. "true" if this item should be used when the application window has focus (the +application is in the foreground), "false" if this item should be used when the application +window does not have focus (for example, if the notification shade is pulled down or a dialog appears).
    +
    +

    Note: Remember that the first item in the state list that +matches the current state of the object will be applied. So if the first item in the list contains +none of the state attributes above, then it will be applied every time, which is why your +default value should always be last (as demonstrated in the following example).

    +
    + +
    +
    + +
    example:
    +
    XML file saved at res/color/button_text.xml: +
    +<?xml version="1.0" encoding="utf-8"?>
    +<selector xmlns:android="http://schemas.android.com/apk/res/android">
    +    <item android:state_pressed="true"
    +          android:color="#ffff0000"/> <!-- pressed -->
    +    <item android:state_focused="true"
    +          android:color="#ff0000ff"/> <!-- focused -->
    +    <item android:color="#ff000000"/> <!-- default -->
    +</selector>
    +
    + +

    This layout XML will apply the color list to a View:

    +
    +<Button
    +    android:layout_width="fill_parent"
    +    android:layout_height="wrap_content"
    +    android:text="@string/button_text"
    +    android:textColor="@color/button_text" />
    +
    +
    + +
    see also:
    +
    + +
    + +
    + + diff --git a/docs/html/guide/topics/resources/drawable-resource.jd b/docs/html/guide/topics/resources/drawable-resource.jd new file mode 100644 index 0000000000000..ec964ed887d46 --- /dev/null +++ b/docs/html/guide/topics/resources/drawable-resource.jd @@ -0,0 +1,711 @@ +page.title=Drawable Resources +parent.title=Resource Types +parent.link=available-resources.html +@jd:body + +
    +
    +

    See also

    +
      +
    1. 2D Graphics
    2. +
    +
    +
    + +

    A drawable resource is a general concept for a graphic that you +can retrieve with {@link android.content.res.Resources#getDrawable(int)} +and draw on the screen. There are several different types of drawables:

    +
    +
    Bitmap File
    +
    A bitmap graphic file ({@code .png}, {@code .jpg}, or {@code .gif}). + A {@link android.graphics.drawable.BitmapDrawable}.
    +
    Nine-Patch File
    +
    A PNG file with stretchable regions to allow image resizing based on content ({@code +.9.png}). A {@link android.graphics.drawable.NinePatchDrawable}.
    + +
    State List
    +
    An XML file that references different bitmap graphics + for different states (for example, to use a different image when a button is pressed). + A {@link android.graphics.drawable.StateListDrawable}.
    +
    Color
    +
    A resource defined in XML that specifies a rectangle of color, with + optionally rounded corners. A {@link android.graphics.drawable.PaintDrawable}.
    +
    Shape
    +
    An XML file that defines a geometric shape, including colors and gradients. + A {@link android.graphics.drawable.ShapeDrawable}.
    +
    + +

    Documentation for the {@link android.graphics.drawable.AnimationDrawable} resource +is in the Animation Resource document.

    + +

    Bitmap File

    + +

    A basic bitmap image. Android supports basic bitmap files in a few different formats: +{@code .png} (preferred), {@code .jpg} (acceptable), {@code .gif} (discouraged).

    + +

    Note: Bitmap files may be automatically optimized with lossless +image compression by the aapt tool. For +example, a true-color PNG that does not require more than 256 colors may be converted to an 8-bit +PNG with a color palette. This will result in an image of equal quality but which requires less +memory. So be aware that the image binaries placed in this directory can change during the build. If +you plan on reading an image as a bit stream in order to convert it to a bitmap, put your images in +the res/raw/ folder instead, where they will not be optimized.

    + +
    + +
    file location:
    +
    res/drawable/filename.png ({@code .png}, {@code .jpg}, or {@code .gif})
    +The filename will be used as the resource ID.
    + +
    compiled resource datatype:
    +
    Resource pointer to a {@link android.graphics.drawable.BitmapDrawable}.
    + +
    resource reference:
    +
    +In Java: R.drawable.filename

  • +In XML: @[package:]drawable/filename + + +
    example:
    +
    With an image saved at res/drawable/myimage.png, this layout XML will apply +the image to a View: +
    +<ImageView
    +    android:layout_height="wrap_content"
    +    android:layout_width="wrap_content"
    +    android:src="@drawable/myimage" />
    +
    +

    This application code will retrieve the image as a {@link +android.graphics.drawable.Drawable}:

    +
    +Resources res = {@link android.content.Context#getResources()};
    +Drawable drawable = res.{@link android.content.res.Resources#getDrawable(int) getDrawable}(R.drawable.myimage);
    +
    +
    + +
    see also:
    +
    + +
    + + + + + + + + + +

    Nine-Patch File

    + +

    A {@link android.graphics.NinePatch} is a PNG image in which you can define stretchable regions +that Android will scale when content within the View exceeds the normal image bounds. You will +typically assign this type of image as the background of a View that has at least one dimension set +to {@code "wrap_content"}, and when the View grows to accomodate the content, the Nine-Patch image +will also be scaled to match the size of the View. An example use of a Nine-Patch image is the +background used by Android's standard {@link android.widget.Button} widget, which must stretch to +accommodate the text (or image) inside the button.

    + +

    For a complete discussion about how to define a Nine-Patch file with stretchable regions, +see the 2D Graphics +document.

    + +
    + +
    file location:
    +
    res/drawable/filename.9.png
    +The filename will be used as the resource ID.
    + +
    compiled resource datatype:
    +
    Resource pointer to a {@link android.graphics.drawable.NinePatchDrawable}.
    + +
    resource reference:
    +
    +In Java: R.drawable.filename
    +In XML: @[package:]drawable/filename +
    + +
    example:
    +
    With an image saved at res/drawable/myninepatch.9.png, this layout XML will +apply the Nine-Patch to a View: +
    +<Button
    +    android:layout_height="wrap_content"
    +    android:layout_width="wrap_content"
    +    android:background="@drawable/myninepatch" />
    +
    +
    + +
    see also:
    +
    +
      +
    • 2D Graphics
    • +
    • {@link android.graphics.drawable.NinePatchDrawable}
    • +
    +
    + +
    + + + + + + + + +

    State List

    + +

    A {@link android.graphics.drawable.StateListDrawable} is a drawable object defined in XML +that uses a several different images to represent the same graphic, depending on the state of +the object. For example, a {@link +android.widget.Button} widget can exist in one of several different states (pressed, focused, +or niether) and, using a state list drawable, you can provide a different button image for each +state.

    + +

    You can describe the state list in an XML file. Each graphic is represented by an {@code +<item>} element inside a single {@code <selector>} element. Each {@code <item>} +uses various attributes to describe the state in which it should be used as the graphic for the +drawable.

    +

    During each state change, the state list is traversed top to bottom and the first item that +matches the current state will be used—the selection is not based on the "best +match," but simply the first item that meets the minimum criteria of the state.

    + +
    + +
    file location:
    +
    res/drawable/filename.xml
    +The filename will be used as the resource ID.
    + +
    compiled resource datatype:
    +
    Resource pointer to a {@link android.graphics.drawable.StateListDrawable}.
    + +
    resource reference:
    +
    +In Java: R.drawable.filename
    +In XML: @[package:]drawable/filename +
    + +
    syntax:
    +
    +
    +<?xml version="1.0" encoding="utf-8"?>
    +<selector xmlns:android="http://schemas.android.com/apk/res/android"
    +    android:constantSize=["true" | "false"]
    +    android:dither=["true" | "false"]
    +    android:variablePadding=["true" | "false"] >
    +    <item
    +        android:drawable="@[package:]drawable/drawable_resource"
    +        android:state_pressed=["true" | "false"]
    +        android:state_focused=["true" | "false"]
    +        android:state_selected=["true" | "false"]
    +        android:state_active=["true" | "false"]
    +        android:state_checkable=["true" | "false"]
    +        android:state_checked=["true" | "false"]
    +        android:state_enabled=["true" | "false"]
    +        android:state_window_focused=["true" | "false"] /> 
    +</selector>
    +
    +
    + +
    elements:
    +
    +
    + +
    <selector>
    +
    Required. This must be the root element. Contains one or more {@code +<item>} elements. +

    attributes:

    +
    +
    xmlns:android
    +
    String. Required. Defines the XML namespace, which must be + "http://schemas.android.com/apk/res/android". +
    android:constantSize
    +
    Boolean. "true" if the drawable's reported internal size will remain constant as the state +changes (the size will be the maximum of all of the states); "false" if the size will vary based on +the current state. Default is false.
    +
    android:dither
    +
    Boolean. "true" to enable dithering of the bitmap if the bitmap does not have the same pixel +configuration as the screen (for instance, an ARGB 8888 bitmap with an RGB 565 screen); "false" to +disable dithering. Default is true.
    +
    android:variablePadding
    +
    Boolean. "true" if the drawable's padding should change based on the current +state that is selected; "false" if the padding should stay the same (based on the maximum +padding of all the states). Enabling this feature requires that you deal with +performing layout when the state changes, which is often not supported. Default is false.
    +
    +
    +
    <item>
    +
    Defines a drawable to use during certain states, as described by its attributes. Must be a +child of a <selector> element. +

    attributes:

    +
    +
    android:drawable
    +
    Drawable resource. Required. Reference to a drawable resource.
    +
    android:state_pressed
    +
    Boolean. "true" if this item should be used when the object is pressed (such as when a button +is touched/clicked); "false" if this item should be used in the default, non-pressed state.
    +
    android:state_focused
    +
    Boolean. "true" if this item should be used when the object is focused (such as when a button +is highlighted using the trackball/d-pad); "false" if this item should be used in the default, +non-focused state.
    +
    android:state_selected
    +
    Boolean. "true" if this item should be used when the object is selected (such as when a +tab is opened); "false" if this item should be used when the object is not selected.
    +
    android:state_checkable
    +
    Boolean. "true" if this item should be used when the object is checkable; "false" if this +item should be used when the object is not checkable. (Only useful if the object can +transition between a checkable and non-checkable widget.)
    +
    android:state_checked
    +
    Boolean. "true" if this item should be used when the object is checked; "false" if it +should be used when the object is un-checked.
    +
    android:state_enabled
    +
    Boolean. "true" if this item should be used when the object is enabled (capable of +receiving touch/click events); "false" if it should be used when the object is disabled.
    +
    android:state_window_focused
    +
    Boolean. "true" if this item should be used when the application window has focus (the +application is in the foreground), "false" if this item should be used when the application +window does not have focus (for example, if the notification shade is pulled down or a dialog appears).
    +
    +

    Note:Remember that the first item in the state list that +matches the current state of the object will be applied. So if the first item in the list contains +none of the state attributes above, then it will be applied every time, which is why your +default value should always be last (as demonstrated in the following example).

    +
    + +
    +
    + +
    example:
    +
    XML file saved at res/drawable/button.xml: +
    +<?xml version="1.0" encoding="utf-8"?>
    +<selector xmlns:android="http://schemas.android.com/apk/res/android">
    +    <item android:state_pressed="true"
    +          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    +    <item android:state_focused="true"
    +          android:drawable="@drawable/button_focused" /> <!-- focused -->
    +    <item android:drawable="@drawable/button_normal" /> <!-- default -->
    +</selector>
    +
    + +

    This layout XML will apply the drawable to a View:

    +
    +<ImageView
    +    android:layout_height="wrap_content"
    +    android:layout_width="wrap_content"
    +    android:src="@drawable/button" />
    +
    +
    + +
    see also:
    +
    +
      +
    • {@link android.graphics.drawable.StateListDrawable}
    • +
    +
    + +
    + + + + + + + + + + + + + + +

    Color

    + +

    This is a color defined in XML that's used as a drawable to fill a rectangular space, +with optionally rounded corners. This kind of drawable behaves like a color fill.

    + +

    Note: A color drawable is a simple resource that is referenced +using the value provided in the {@code name} attribute (not the name of the XML file). As +such, you can combine a color drawable resources with other simple resources in the one XML file, +under one {@code <resources>} element.

    + + +
    + +
    file location:
    +
    res/drawable/filename.png
    +The filename is arbitrary. The element's {@code name} will be used as the resource ID.
    + +
    compiled resource datatype:
    +
    Resource pointer to a {@link android.graphics.drawable.PaintDrawable}.
    + +
    resource reference:
    +
    +In Java: R.drawable.color_name
    +In XML: @[package:]drawable/color_name +
    + +
    syntax:
    +
    +
    +<?xml version="1.0" encoding="utf-8"?>
    +<resources>
    +    <drawable
    +        name="color_name"
    +        >color</drawable>
    +</resources>
    +
    +
    + +
    elements:
    +
    +
    + +
    <resources>
    +
    Required. This must be the root node. +

    No attributes.

    +
    +
    <drawable>
    +
    A color to use as a drawable rectangle. The value can be + any valid hexadecimal color value or a color + resource. A color value always begins with a pound (#) character, followed + by the Alpha-Red-Green-Blue information in one of the following formats: + #RGB, #RRGGBB, #ARGB, or #AARRGGBB. +

    attributes:

    +
    +
    name
    +
    String. Required. + A name for the color. This name will be used as the resource ID.
    +
    + +
    + +
    +
    + +
    example:
    +
    XML file saved at res/drawable/colors.xml: +
    +<?xml version="1.0" encoding="utf-8"?>
    +<resources>
    +    <drawable name="solid_red">#f00</drawable>
    +    <drawable name="solid_blue">#0000ff</drawable>
    +</resources>
    +
    +

    This layout XML will apply a color drawable to a View:

    +
    +<TextView
    +    android:layout_width="fill_parent"
    +    android:layout_height="wrap_content"
    +    android:background="@drawable/solid_blue" />
    +
    +

    This application code will get a color drawable and apply it to a View:

    +
    +Resources res =  {@link android.content.Context#getResources()};
    +Drawable redDrawable = res.{@link android.content.res.Resources#getDrawable(int) getDrawable}(R.drawable.solid_red);
    +
    +TextView tv = (TextView) findViewByID(R.id.text);
    +tv.setBackground(redDrawable);
    +
    +
    + +
    see also:
    +
    +
      +
    • {@link android.graphics.drawable.PaintDrawable}
    • +
    +
    + +
    + + + + + + + + + + + + +

    Shape

    + +

    This is a generic shape defined in XML.

    + +
    + +
    file location:
    +
    res/drawable/filename.xml
    +The filename will be used as the resource ID.
    + +
    compiled resource datatype:
    +
    Resource pointer to a {@link android.graphics.drawable.ShapeDrawable}.
    + +
    resource reference:
    +
    +In Java: R.drawable.filename
    +In XML: @[package:]drawable/filename +
    + +
    syntax:
    +
    +
    +<?xml version="1.0" encoding="utf-8"?>
    +<shape xmlns:android="http://schemas.android.com/apk/res/android"
    +    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    +    <gradient
    +        android:angle="integer"
    +        android:centerX="integer"
    +        android:centerY="integer"
    +        android:centerColor="integer"
    +        android:gradientRadius="integer"
    +        android:type=""
    +        android:usesLevel=""
    +        android:startColor="color"
    +        android:endColor="color" />
    +    <solid
    +        android:color="color" />
    +    <stroke
    +        android:width="integer"
    +        android:color="color"
    +        android:dashWidth="integer"
    +        android:dashGap="integer" />
    +    <padding
    +        android:left="integer"
    +        android:top="integer"
    +        android:right="integer"
    +        android:bottom="integer" />
    +    <corners
    +        android:radius="integer"
    +        android:topLeftRadius="integer"
    +        android:topRightRadius="integer"
    +        android:bottomLeftRadius="integer"
    +        android:bottomRightRadius="integer" />
    +</shape>
    +
    +
    + +
    elements:
    +
    +
    + +
    <shape>
    +
    Required. This must be the root element. +

    attributes:

    +
    +
    android:shape
    +
    Keyword. Defines the type of shape. Valid values are: + + + + + + + + + + +
    ValueDesciption
    {@code "rectangle"}A rectangle that fills the containing View. This is the default shape.
    {@code "oval"}An oval shape that fits the dimensions of the containing View.
    {@code "line"}A horizontal line that spans the width of the containing View. This + shape requires the {@code <stroke>} element to define the width of the + line.
    {@code "ring"}A ring shape.
    +
    +
    +

    The following attributes are used only when {@code android:shape="ring"}:

    +
    +
    android:innerRadius
    +
    Dimension. The radius for the +inner part of the ring (the hole in the middle), as a dimension value or dimension resource.
    +
    android:innerRadiusRatio
    +
    Float. The radius for the inner +part of the ring, expressed as a ratio of the ring's width. For instance, if {@code +android:innerRadiusRatio="5"}, then the inner radius equals the ring's width divided by 5. This +value will be overridden by {@code android:innerRadius}. Default value is 9.
    +
    android:thickness
    +
    Dimension. The thickness of the +ring, as a dimension value or dimension resource.
    +
    android:thicknessRatio
    +
    Float. The thickness of the ring, +expressed as a ratio of the ring's width. For instance, if {@code android:thicknessRatio="2"}, then +the thickness equals the ring's width divided by 2. This value will be overridden by {@code +android:innerRadius}. Default value is 3.
    +
    android:useLevel
    +
    Boolean. "true" if this is used as +a {@link android.graphics.drawable.LevelListDrawable}. This should normally be "false" + or your shape may not appear.
    +
    +
    <gradient>
    +
    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.
    +
    android:centerX
    +
    Float. The relative X-position for the center of the gradient (0 - 1.0). +Does not apply when {@code android:type="linear"}.
    +
    android:centerY
    +
    Float. The relative Y-position for the center of the gradient (0 - 1.0). +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.
    +
    +
    +
    <solid>
    +
    A solid color to fill the shape. +

    attributes:

    +
    +
    android:color
    +
    Color. The color to apply to the shape, as a hexadecimal +value or color resource.
    +
    +
    +
    <stroke>
    +
    A stroke line for the shape. +

    attributes:

    +
    +
    android:width
    +
    Dimension. The thickness of the line, as a dimension value or dimension resource.
    +
    android:color
    +
    Color. The color of the line, as a +hexadecimal value or color resource.
    +
    android:dashGap
    +
    Dimension. The distance between line dashes, as a dimension value or dimension resource. Only valid if {@code +android:dashWidth} is set.
    +
    android:dashWidth
    +
    Dimension. The size of each dash line, as a dimension value or dimension resource. Only valid if {@code +android:dashGap} is set.
    +
    +
    +
    <padding>
    +
    Padding to apply to the containing View element (this pads the position of the View +content, not the shape). +

    attributes:

    +
    +
    android:left
    +
    Dimension. Left padding, as a dimension value or dimension resource.
    +
    android:top
    +
    Dimension. Top padding, as a dimension value or dimension resource.
    +
    android:right
    +
    Dimension. Right padding, as a dimension value or dimension resource.
    +
    android:bottom
    +
    Dimension. Bottom padding, as a dimension value or dimension resource.
    +
    +
    +
    <corners>
    +
    Creates rounded corners for the shape. Applies only when the shape is a rectangle. +

    attributes:

    +
    +
    android:radius
    +
    Dimension. The radius for all corners, as a dimension value or dimension resource. This will be overridden for each +corner by the following attributes.
    +
    android:topLeftRadius
    +
    Dimension. The radius for the top-left corner, as a dimension value or dimension resource.
    +
    android:topRightRadius
    +
    Dimension. The radius for the top-right corner, as a dimension value or dimension resource.
    +
    android:bottomLeftRadius
    +
    Dimension. The radius for the bottom-left corner, as a dimension value or dimension resource.
    +
    android:bottomRightRadius
    +
    Dimension. The radius for the bottom-right corner, as a dimension value or dimension resource.
    +
    +

    Note: Every corner must (initially) be provided a corner +radius greater than zero, or else no corners will be rounded. If you want specific corners +to not be rounded, a work-around is to use {@code android:radius} to set a default corner +radius greater than zero, but then override each and every corner with the values you really +want, providing zero ("0dp") where you don't want rounded corners.

    +
    + +
    +
    + +
    example:
    +
    XML file saved at res/drawable/gradient_box.xml: +
    +<?xml version="1.0" encoding="utf-8"?>
    +<shape xmlns:android="http://schemas.android.com/apk/res/android"
    +    android:shape="rectangle">
    +    <gradient 
    +        android:startColor="#FFFF0000" 
    +        android:endColor="#80FF00FF"
    +        android:angle="45"/>
    +    <padding android:left="7dp" 
    +        android:top="7dp"
    +        android:right="7dp" 
    +        android:bottom="7dp" />
    +    <corners android:radius="8dp" />
    +</shape>
    +
    +

    This layout XML will apply the shape drawable to a View:

    +
    +<TextView
    +    android:background="@drawable/gradient_box"
    +    android:layout_height="wrap_content"
    +    android:layout_width="wrap_content" />
    +
    +

    This application code will get the shape drawable and apply it to a View:

    +
    +Resources res = {@link android.content.Context#getResources()};
    +Drawable shape = res. {@link android.content.res.Resources#getDrawable(int) getDrawable}(R.drawable.gradient_box);
    +
    +TextView tv = (TextView)findViewByID(R.id.textview);
    +tv.setBackground(shape);
    +
    +
    + +
    + + + + + + + + + + + + + diff --git a/docs/html/guide/topics/resources/index.jd b/docs/html/guide/topics/resources/index.jd index 1425cfaa1af2f..f602a0419c96b 100644 --- a/docs/html/guide/topics/resources/index.jd +++ b/docs/html/guide/topics/resources/index.jd @@ -1,40 +1,104 @@ -page.title=Resources and Assets +page.title=Application Resources @jd:body
    - -

    Key classes

    +

    Topics

      -
    1. {@link android.content.res.Resources}
    2. -
    3. {@link android.content.res.AssetManager}
    4. +
    5. Providing Resources
    6. +
    7. Accessing Resources
    8. +
    9. Handling Runtime Changes
    10. +
    11. Localization
    +

    Reference

    +
      +
    1. Resource Types
    2. +
    -

    Resources are an integral part of an Android application. -In general, these are external elements that you want to include and reference within your application, -like images, audio, video, text strings, layouts, themes, etc. Every Android application contains -a directory for resources (res/) and a directory for assets (assets/). -Assets are used less often, because their applications are far fewer. You only need to save data -as an asset when you need to read the raw bytes. The directories for resources and assets both -reside at the top of an Android project tree, at the same level as your source code directory -(src/).

    -

    The difference between "resources" and "assets" isn't much on the surface, but in general, -you'll use resources to store your external content much more often than you'll use assets. -The real difference is that anything -placed in the resources directory will be easily accessible from your application from the -R class, which is compiled by Android. Whereas, anything placed in the assets -directory will maintain its raw file format and, in order to read it, you must use the -{@link android.content.res.AssetManager} to read the file as a stream of bytes. So keeping -files and data in resources (res/) makes them easily accessible.

    +

    You should always externalize resources such as images and strings from your application +code, so that you can maintain them independently. Externalizing your +resources also allows you to provide alternative resources that support specific device +configurations such as different languages or screen sizes, which becomes increasingly +important as more Android-powered devices become available with different configurations. In order +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.

    -

    Within the documents of this topic, you'll find information on the kinds of standard resources -that are typically used in an Android application and how to reference them from you code. -Resources and Internationalization -is where you should start, to learn more about how Android utilizes project resources. Then, the -Available Resource Types -document offers a summary of various resource types and a reference to their specifications. -

    +
    + +

    +Figure 1. Two device configurations, both using default +resources.

    +
    + +
    + +

    +Figure 2. Two device configurations, one using alternative +resources.

    +
    + +

    For any type of resource, you can specify default and multiple +alternative resources for your application:

    + + +

    For example, while your default UI +layout is saved in the {@code res/layout/} directory, you might specify a different UI layout to +be used when the screen is in landscape orientation, by saving it in the {@code res/layout-land/} +directory. The Android system will automatically apply the appropriate resources by matching the +device's current configuration to your resource directory names.

    + +

    Figure 1 demonstrates how a collection of default resources from an application will be applied +to two different devices when there are no alternative resources available. Figure 2 shows +the same application with a set of alternative resources that qualify for one of the device +configurations, thus, the two devices uses different resources.

    + +

    The information above is just an introduction to how application resources work on Android. +The following documents provide a complete guide to how you can organize your application resources, +specify alternative resources, access them in your application, and more:

    + +
    +
    Providing Resources
    +
    What kinds of resources you can provide in your app, where to save them, and how to create +alternative resources for specific device configurations.
    +
    Accessing Resources
    +
    How to use the resources you've provided, either by referencing them from your application +code or from other XML resources.
    +
    Handling Runtime Changes
    +
    How to manage configuration changes that occur while your Activity is running.
    +
    Localization
    +
    A bottom-up guide to localizing your application using alternative resources. While this is +just one specific use of alternative resources, it is very important in order to reach more +users.
    +
    Resource Types
    +
    A reference of various resource types you can provide, describing their XML elements, +attributes, and syntax. For example, this reference shows you how to create a resource for +application menus, drawables, animations, and more.
    +
    + + diff --git a/docs/html/guide/topics/resources/layout-resource.jd b/docs/html/guide/topics/resources/layout-resource.jd new file mode 100644 index 0000000000000..a6b177f01fc6b --- /dev/null +++ b/docs/html/guide/topics/resources/layout-resource.jd @@ -0,0 +1,224 @@ +page.title=Layout Resource +parent.title=Resource Types +parent.link=available-resources.html +@jd:body + +
    +
    +

    See also

    +
      +
    1. Declaring Layout
    2. +
    +
    +
    + +

    A layout resource defines the architecture for the UI in an Activity or a component of a UI.

    + + +
    + +
    file location:
    +
    res/layout/filename.xml
    +The filename will be used as the resource ID.
    + +
    compiled resource datatype:
    +
    Resource pointer to a {@link android.view.View} (or subclass) resource.
    + +
    resource reference:
    +
    +In Java: R.layout.filename
    +In XML: @[package:]layout/filename +
    + +
    syntax:
    +
    +
    +<?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] >
    +    <View
    +        android:id="@+id/name"
    +        android:layout_height="@+id/string_name"
    +        android:layout_width="@+id/string_name"
    +        [other attributes] >
    +        <requestFocus/>
    +    </View>
    +    <ViewGroup >
    +        <View />
    +    </ViewGroup>
    +</ViewGroup>
    +
    +

    Note: The root element can be either a +{@link android.view.ViewGroup} or a {@link android.view.View}, but there must be only +one root element and it must contain the {@code xmlns:android} attribute with the {@code android} +namespace as shown.

    +
    + +
    elements:
    +
    +
    + +
    <ViewGroup>
    +
    A container for other {@link android.view.View} elements. There are many + different kinds of {@link android.view.ViewGroup} objects and each one lets you + specify the layout of the child elements in different ways. Different kinds of + {@link android.view.ViewGroup} objects include {@link android.widget.LinearLayout}, + {@link android.widget.RelativeLayout}, and {@link android.widget.FrameLayout}. +

    You should not assume that any derivation of {@link android.view.ViewGroup} + will accept nested {@link android.view.View}s. Some {@link android.view.ViewGroup}s + are implementations of the {@link android.widget.AdapterView} class, which determines + its children only from an {@link android.widget.Adapter}.

    +

    attributes:

    +
    +
    android:id
    +
    Resource name. A unique resource name for the element, which you can +use to obtain a reference to the {@link android.view.ViewGroup} from your application. + The value takes the form: "@+id/name". See more about the + value for {@code android:id} below. +
    +
    android:layout_height
    +
    Dimension or keyword. Required. The height for the group, as a +dimension value (or dimension resource) or a keyword ({@code "fill_parent"} +or {@code "wrap_content"}). See the valid values below. +
    +
    android:layout_width
    +
    Dimension or keyword. Required. The width for the group, as a +dimension value (or dimension resource) or a keyword ({@code "fill_parent"} +or {@code "wrap_content"}). See the valid values below. +
    +
    +

    More attributes are supported by the {@link android.view.ViewGroup} + base class, and many more are supported by each implementation of + {@link android.view.ViewGroup}. For a reference of all available attributes, + see the corresponding reference documentation for the {@link android.view.ViewGroup} class +(for example, the LinearLayout XML attributes).

    +
    +
    <View>
    +
    An individual UI component, generally referred to as a "widget". Different + kinds of {@link android.view.View} objects include {@link android.widget.TextView}, + {@link android.widget.Button}, and {@link android.widget.CheckBox}. +

    attributes:

    +
    +
    android:id
    +
    Resource name. A unique resource name for the element, which you can use to + obtain a reference to the {@link android.view.View} from your application. + The value takes the form: "@+id/name". See more about the + value for {@code android:id} below. +
    +
    android:layout_height
    +
    Dimension or keyword. Required. The height for the element, as +a dimension value (or dimension resource) or a keyword ({@code "fill_parent"} +or {@code "wrap_content"}). See the valid values below. +
    +
    android:layout_width
    +
    Dimension or keyword. Required. The width for the element, as +a dimension value (or dimension resource) or a keyword ({@code "fill_parent"} +or {@code "wrap_content"}). See the valid values below. +
    +
    +

    More attributes are supported by the {@link android.view.View} + base class, and many more are supported by each implementation of + {@link android.view.View}. Read Declaring + Layout for more information. For a reference of all available attributes, + see the corresponding reference documentation (for example, the TextView XML attributes).

    +
    +
    <requestFocus>
    +
    Any element representing a {@link android.view.View} object can include this empty element, + which gives it's parent initial focus on the screen. You can have only one of these + elements per file.
    + +
    + +

    Value for android:id

    + +

    For the ID value, you should use this syntax form: "@+id/name". The plus symbol, +{@code +}, indicates that this is a new resource ID and the aapt tool will create +a new resource number to the {@code R.java} class, if it doesn't already exist. For example:

    +
    +<TextView android:id="@+id/nameTextbox"/>
    +
    +

    You can then refer to it this way in Java:

    +
    +findViewById(R.id.nameTextbox);
    +
    + +

    Value for android:layout_height and +android:layout_width:

    + +

    The height and width value can be expressed using any of the + dimension + units supported by Android (px, dp, sp, pt, in, mm) or with the following keywords:

    + + + + + + + + + + + + +
    ValueDescription
    match_parentSets the dimension to match that of the parent element. Added in API Level 8 to +deprecate fill_parent.
    fill_parentSets the dimension to match that of the parent element.
    wrap_contentSets the dimension only to the size required to fit the content of this element.
    + +

    Custom View elements

    + +

    You can create your own custom {@link android.view.View} and {@link android.view.ViewGroup} +elements and apply them to your layout the same as a standard layout +element. You can also specify the attributes supported in the XML element. To learn more, +read Building Custom Components. +

    + +
    + +
    example:
    +
    XML file saved at res/layout/main_activity.xml: +
    +<?xml version="1.0" encoding="utf-8"?>
    +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    +              android:layout_width="fill_parent" 
    +              android:layout_height="fill_parent" 
    +              android:orientation="vertical" >
    +    <TextView android:id="@+id/text"
    +              android:layout_width="wrap_content"
    +              android:layout_height="wrap_content"
    +              android:text="Hello, I am a TextView" />
    +    <Button android:id="@+id/button"
    +            android:layout_width="wrap_content"
    +            android:layout_height="wrap_content"
    +            android:text="Hello, I am a Button" />
    +</LinearLayout>
    +
    +

    This application code will load the layout for an {@link android.app.Activity}, in the + {@link android.app.Activity#onCreate(Bundle) onCreate()} method: +

    +
    +public void onCreate(Bundle savedInstanceState) {
    +    super.onCreate(savedInstanceState);
    +    setContentView.(R.layout.main_activity);
    +}
    +
    +
    + + +
    see also:
    +
    +
      +
    • Declaring Layout
    • +
    • {@link android.view.View}
    • +
    • {@link android.view.ViewGroup}
    • +
    +
    + +
    \ No newline at end of file diff --git a/docs/html/guide/topics/resources/localization.jd b/docs/html/guide/topics/resources/localization.jd index 192695b722789..e76a92cb31ea8 100755 --- a/docs/html/guide/topics/resources/localization.jd +++ b/docs/html/guide/topics/resources/localization.jd @@ -1,5 +1,5 @@ page.title=Localization -parent.title=Resources and Assets +parent.title=Application Resources parent.link=index.html @jd:body @@ -25,12 +25,11 @@ defaults.
    1. Why Default Resources Are Important
  • Using Resources for Localization
      -
    1. How to Create Default Resources
    2. - How to Create Alternate Resources
    3. +
    4. How to Create Default Resources
    5. +
    6. How to Create Alternative Resources
    7. Which Resources Take Precedence?
    8. Referring to Resources in Java
    9. - -
    +
  • Localization Strategies
  • Testing Localized Applications
  • @@ -52,7 +51,7 @@ defaults.
    1. Hello, L10N Tutorial
    2. -
    3. Resources
    4. +
    5. Providing Resources
    6. Declaring Layout
    7. Activity Lifecycle
    @@ -80,7 +79,7 @@ functionality: