to top
Android APIs
public final class

Resources.Theme

extends Object
java.lang.Object
   ↳ android.content.res.Resources.Theme

Class Overview

This class holds the current attribute values for a particular theme. In other words, a Theme is a set of values for resource attributes; these are used in conjunction with TypedArray to resolve the final value for an attribute.

The Theme's attributes come into play in two ways: (1) a styled attribute can explicit reference a value in the theme through the "?themeAttribute" syntax; (2) if no value has been defined for a particular styled attribute, as a last resort we will try to find that attribute's value in the Theme.

You will normally use the obtainStyledAttributes(AttributeSet, int[], int, int) APIs to retrieve XML attributes with style and theme information applied.

Summary

Public Methods
void applyStyle(int resid, boolean force)
Place new attribute values into the theme.
void dump(int priority, String tag, String prefix)
Print contents of this theme out to the log.
TypedArray obtainStyledAttributes(int[] attrs)
Return a TypedArray holding the values defined by Theme which are listed in attrs.
TypedArray obtainStyledAttributes(int resid, int[] attrs)
Return a TypedArray holding the values defined by the style resource resid which are listed in attrs.
TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)
Return a TypedArray holding the attribute values in set that are listed in attrs.
boolean resolveAttribute(int resid, TypedValue outValue, boolean resolveRefs)
Retrieve the value of an attribute in the Theme.
void setTo(Resources.Theme other)
Set this theme to hold the same contents as the theme other.
Protected Methods
void finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public void applyStyle (int resid, boolean force)

Added in API level 1

Place new attribute values into the theme. The style resource specified by resid will be retrieved from this Theme's resources, its values placed into the Theme object.

The semantics of this function depends on the force argument: If false, only values that are not already defined in the theme will be copied from the system resource; otherwise, if any of the style's attributes are already defined in the theme, the current values in the theme will be overwritten.

Parameters
resid The resource ID of a style resource from which to obtain attribute values.
force If true, values in the style resource will always be used in the theme; otherwise, they will only be used if not already defined in the theme.

public void dump (int priority, String tag, String prefix)

Added in API level 1

Print contents of this theme out to the log. For debugging only.

Parameters
priority The log priority to use.
tag The log tag to use.
prefix Text to prefix each line printed.

public TypedArray obtainStyledAttributes (int[] attrs)

Added in API level 1

Return a TypedArray holding the values defined by Theme which are listed in attrs.

Be sure to call TypedArray.recycle() when you are done with the array.

Parameters
attrs The desired attributes.
Returns
  • Returns a TypedArray holding an array of the attribute values. Be sure to call TypedArray.recycle() when done with it.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

public TypedArray obtainStyledAttributes (int resid, int[] attrs)

Added in API level 1

Return a TypedArray holding the values defined by the style resource resid which are listed in attrs.

Be sure to call TypedArray.recycle() when you are done with the array.

Parameters
resid The desired style resource.
attrs The desired attributes in the style.
Returns
  • Returns a TypedArray holding an array of the attribute values. Be sure to call TypedArray.recycle() when done with it.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

public TypedArray obtainStyledAttributes (AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)

Added in API level 1

Return a TypedArray holding the attribute values in set that are listed in attrs. In addition, if the given AttributeSet specifies a style class (through the "style" attribute), that style will be applied on top of the base attributes it defines.

Be sure to call TypedArray.recycle() when you are done with the array.

When determining the final value of a particular attribute, there are four inputs that come into play:

  1. Any attribute values in the given AttributeSet.
  2. The style resource specified in the AttributeSet (named "style").
  3. The default style specified by defStyleAttr and defStyleRes
  4. The base values in this theme.

Each of these inputs is considered in-order, with the first listed taking precedence over the following ones. In other words, if in the AttributeSet you have supplied <Button textColor="#ff000000">, then the button's text will always be black, regardless of what is specified in any of the styles.

Parameters
set The base set of attribute values. May be null.
attrs The desired attributes to be retrieved.
defStyleAttr An attribute in the current theme that contains a reference to a style resource that supplies defaults values for the TypedArray. Can be 0 to not look for defaults.
defStyleRes A resource identifier of a style resource that supplies default values for the TypedArray, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.
Returns
  • Returns a TypedArray holding an array of the attribute values. Be sure to call TypedArray.recycle() when done with it.

public boolean resolveAttribute (int resid, TypedValue outValue, boolean resolveRefs)

Added in API level 1

Retrieve the value of an attribute in the Theme. The contents of outValue are ultimately filled in by getValue(int, TypedValue, boolean).

Parameters
resid The resource identifier of the desired theme attribute.
outValue Filled in with the ultimate resource value supplied by the attribute.
resolveRefs If true, resource references will be walked; if false, outValue may be a TYPE_REFERENCE. In either case, it will never be a TYPE_ATTRIBUTE.
Returns
  • boolean Returns true if the attribute was found and outValue is valid, else false.

public void setTo (Resources.Theme other)

Added in API level 1

Set this theme to hold the same contents as the theme other. If both of these themes are from the same Resources object, they will be identical after this function returns. If they are from different Resources, only the resources they have in common will be set in this theme.

Parameters
other The existing Theme to copy from.

Protected Methods

protected void finalize ()

Added in API level 1

Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override finalize are significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicit close method (and implement Closeable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like a BigInteger where typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.

If you must use finalizers, consider at least providing your own ReferenceQueue and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for calling super.finalize() yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.

Throws
Throwable