Android APIs
public class

TypedArray

extends Object
java.lang.Object
   ↳ android.content.res.TypedArray

Class Overview

Container for an array of values that were retrieved with obtainStyledAttributes(AttributeSet, int[], int, int) or obtainAttributes(AttributeSet, int[]). Be sure to call recycle() when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.

Summary

Public Methods
boolean getBoolean(int index, boolean defValue)
Retrieve the boolean value for the attribute at index.
int getChangingConfigurations()
Return a mask of the configuration parameters for which the values in this typed array may change.
int getColor(int index, int defValue)
Retrieve the color value for the attribute at index.
ColorStateList getColorStateList(int index)
Retrieve the ColorStateList for the attribute at index.
float getDimension(int index, float defValue)
Retrieve a dimensional unit attribute at index.
int getDimensionPixelOffset(int index, int defValue)
Retrieve a dimensional unit attribute at index for use as an offset in raw pixels.
int getDimensionPixelSize(int index, int defValue)
Retrieve a dimensional unit attribute at index for use as a size in raw pixels.
Drawable getDrawable(int index)
Retrieve the Drawable for the attribute at index.
float getFloat(int index, float defValue)
Retrieve the float value for the attribute at index.
float getFraction(int index, int base, int pbase, float defValue)
Retrieve a fractional unit attribute at index.
int getIndex(int at)
Return an index in the array that has data.
int getIndexCount()
Return the number of indices in the array that actually have data.
int getInt(int index, int defValue)
Retrieve the integer value for the attribute at index.
int getInteger(int index, int defValue)
Retrieve the integer value for the attribute at index.
int getLayoutDimension(int index, String name)
Special version of getDimensionPixelSize(int, int) for retrieving ViewGroup's layout_width and layout_height attributes.
int getLayoutDimension(int index, int defValue)
Special version of getDimensionPixelSize(int, int) for retrieving ViewGroup's layout_width and layout_height attributes.
String getNonResourceString(int index)
Retrieve the string value for the attribute at index, but only if that string comes from an immediate value in an XML file.
String getPositionDescription()
Returns a message about the parser state suitable for printing error messages.
int getResourceId(int index, int defValue)
Retrieve the resource identifier for the attribute at index.
Resources getResources()
Return the Resources object this array was loaded from.
String getString(int index)
Retrieve the string value for the attribute at index.
CharSequence getText(int index)
Retrieve the styled string value for the attribute at index.
CharSequence[] getTextArray(int index)
Retrieve the CharSequence[] for the attribute at index.
int getType(int index)
Returns the type of attribute at the specified index.
boolean getValue(int index, TypedValue outValue)
Retrieve the raw TypedValue for the attribute at index.
boolean hasValue(int index)
Determines whether there is an attribute at index.
boolean hasValueOrEmpty(int index)
Determines whether there is an attribute at index, returning true if the attribute was explicitly set to @empty and false only if the attribute was undefined.
int length()
Return the number of values in this array.
TypedValue peekValue(int index)
Retrieve the raw TypedValue for the attribute at index and return a temporary object holding its data.
void recycle()
Recycle the TypedArray, to be re-used by a later caller.
String toString()
Returns a string containing a concise, human-readable description of this object.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public boolean getBoolean (int index, boolean defValue)

Added in API level 1

Retrieve the boolean value for the attribute at index.

Parameters
index Index of attribute to retrieve.
defValue Value to return if the attribute is not defined.
Returns
  • Attribute boolean value, or defValue if not defined.

public int getChangingConfigurations ()

Added in API level 21

Return a mask of the configuration parameters for which the values in this typed array may change.

Returns
  • Returns a mask of the changing configuration parameters, as defined by ActivityInfo.
See Also

public int getColor (int index, int defValue)

Added in API level 1

Retrieve the color value for the attribute at index. If the attribute references a color resource holding a complex ColorStateList, then the default color from the set is returned.

Parameters
index Index of attribute to retrieve.
defValue Value to return if the attribute is not defined or not a resource.
Returns
  • Attribute color value, or defValue if not defined.

public ColorStateList getColorStateList (int index)

Added in API level 1

Retrieve the ColorStateList for the attribute at index. The value may be either a single solid color or a reference to a color or complex ColorStateList description.

Parameters
index Index of attribute to retrieve.
Returns
  • ColorStateList for the attribute, or null if not defined.

public float getDimension (int index, float defValue)

Added in API level 1

Retrieve a dimensional unit attribute at index. Unit conversions are based on the current DisplayMetrics associated with the resources this TypedArray object came from.

Parameters
index Index of attribute to retrieve.
defValue Value to return if the attribute is not defined or not a resource.
Returns
  • Attribute dimension value multiplied by the appropriate metric, or defValue if not defined.

public int getDimensionPixelOffset (int index, int defValue)

Added in API level 1

Retrieve a dimensional unit attribute at index for use as an offset in raw pixels. This is the same as getDimension(int, float), except the returned value is converted to integer pixels for you. An offset conversion involves simply truncating the base value to an integer.

Parameters
index Index of attribute to retrieve.
defValue Value to return if the attribute is not defined or not a resource.
Returns
  • Attribute dimension value multiplied by the appropriate metric and truncated to integer pixels, or defValue if not defined.

public int getDimensionPixelSize (int index, int defValue)

Added in API level 1

Retrieve a dimensional unit attribute at index for use as a size in raw pixels. This is the same as getDimension(int, float), except the returned value is converted to integer pixels for use as a size. A size conversion involves rounding the base value, and ensuring that a non-zero base value is at least one pixel in size.

Parameters
index Index of attribute to retrieve.
defValue Value to return if the attribute is not defined or not a resource.
Returns
  • Attribute dimension value multiplied by the appropriate metric and truncated to integer pixels, or defValue if not defined.

public Drawable getDrawable (int index)

Added in API level 1

Retrieve the Drawable for the attribute at index.

Parameters
index Index of attribute to retrieve.
Returns
  • Drawable for the attribute, or null if not defined.

public float getFloat (int index, float defValue)

Added in API level 1

Retrieve the float value for the attribute at index.

Parameters
index Index of attribute to retrieve.
Returns
  • Attribute float value, or defValue if not defined..

public float getFraction (int index, int base, int pbase, float defValue)

Added in API level 1

Retrieve a fractional unit attribute at index.

Parameters
index Index of attribute to retrieve.
base The base value of this fraction. In other words, a standard fraction is multiplied by this value.
pbase The parent base value of this fraction. In other words, a parent fraction (nn%p) is multiplied by this value.
defValue Value to return if the attribute is not defined or not a resource.
Returns
  • Attribute fractional value multiplied by the appropriate base value, or defValue if not defined.

public int getIndex (int at)

Added in API level 1

Return an index in the array that has data.

Parameters
at The index you would like to returned, ranging from 0 to getIndexCount().
Returns

public int getIndexCount ()

Added in API level 1

Return the number of indices in the array that actually have data.

public int getInt (int index, int defValue)

Added in API level 1

Retrieve the integer value for the attribute at index.

Parameters
index Index of attribute to retrieve.
defValue Value to return if the attribute is not defined.
Returns
  • Attribute int value, or defValue if not defined.

public int getInteger (int index, int defValue)

Added in API level 1

Retrieve the integer value for the attribute at index.

Parameters
index Index of attribute to retrieve.
defValue Value to return if the attribute is not defined or not a resource.
Returns
  • Attribute integer value, or defValue if not defined.

public int getLayoutDimension (int index, String name)

Added in API level 1

Special version of getDimensionPixelSize(int, int) for retrieving ViewGroup's layout_width and layout_height attributes. This is only here for performance reasons; applications should use getDimensionPixelSize(int, int).

Parameters
index Index of the attribute to retrieve.
name Textual name of attribute for error reporting.
Returns
  • Attribute dimension value multiplied by the appropriate metric and truncated to integer pixels.

public int getLayoutDimension (int index, int defValue)

Added in API level 3

Special version of getDimensionPixelSize(int, int) for retrieving ViewGroup's layout_width and layout_height attributes. This is only here for performance reasons; applications should use getDimensionPixelSize(int, int).

Parameters
index Index of the attribute to retrieve.
defValue The default value to return if this attribute is not default or contains the wrong type of data.
Returns
  • Attribute dimension value multiplied by the appropriate metric and truncated to integer pixels.

public String getNonResourceString (int index)

Added in API level 1

Retrieve the string value for the attribute at index, but only if that string comes from an immediate value in an XML file. That is, this does not allow references to string resources, string attributes, or conversions from other types. As such, this method will only return strings for TypedArray objects that come from attributes in an XML file.

Parameters
index Index of attribute to retrieve.
Returns
  • String holding string data. Any styling information is removed. Returns null if the attribute is not defined or is not an immediate string value.

public String getPositionDescription ()

Added in API level 1

Returns a message about the parser state suitable for printing error messages.

public int getResourceId (int index, int defValue)

Added in API level 1

Retrieve the resource identifier for the attribute at index. Note that attribute resource as resolved when the overall TypedArray object is retrieved. As a result, this function will return the resource identifier of the final resource value that was found, not necessarily the original resource that was specified by the attribute.

Parameters
index Index of attribute to retrieve.
defValue Value to return if the attribute is not defined or not a resource.
Returns
  • Attribute resource identifier, or defValue if not defined.

public Resources getResources ()

Added in API level 1

Return the Resources object this array was loaded from.

public String getString (int index)

Added in API level 1

Retrieve the string value for the attribute at index.

Parameters
index Index of attribute to retrieve.
Returns
  • String holding string data. Any styling information is removed. Returns null if the attribute is not defined.

public CharSequence getText (int index)

Added in API level 1

Retrieve the styled string value for the attribute at index.

Parameters
index Index of attribute to retrieve.
Returns
  • CharSequence holding string data. May be styled. Returns null if the attribute is not defined.

public CharSequence[] getTextArray (int index)

Added in API level 1

Retrieve the CharSequence[] for the attribute at index. This gets the resource ID of the selected attribute, and uses Resources.getTextArray of the owning Resources object to retrieve its String[].

Parameters
index Index of attribute to retrieve.
Returns
  • CharSequence[] for the attribute, or null if not defined.

public int getType (int index)

Added in API level 21

Returns the type of attribute at the specified index.

Parameters
index Index of attribute whose type to retrieve.
Returns
  • Attribute type.

public boolean getValue (int index, TypedValue outValue)

Added in API level 1

Retrieve the raw TypedValue for the attribute at index.

Parameters
index Index of attribute to retrieve.
outValue TypedValue object in which to place the attribute's data.
Returns
  • Returns true if the value was retrieved, else false.

public boolean hasValue (int index)

Added in API level 1

Determines whether there is an attribute at index.

Note: If the attribute was set to @empty or @undefined, this method returns false.

Parameters
index Index of attribute to retrieve.
Returns
  • True if the attribute has a value, false otherwise.

public boolean hasValueOrEmpty (int index)

Added in API level 22

Determines whether there is an attribute at index, returning true if the attribute was explicitly set to @empty and false only if the attribute was undefined.

Parameters
index Index of attribute to retrieve.
Returns
  • True if the attribute has a value or is empty, false otherwise.

public int length ()

Added in API level 1

Return the number of values in this array.

public TypedValue peekValue (int index)

Added in API level 1

Retrieve the raw TypedValue for the attribute at index and return a temporary object holding its data. This object is only valid until the next call on to TypedArray.

Parameters
index Index of attribute to retrieve.
Returns
  • Returns a TypedValue object if the attribute is defined, containing its data; otherwise returns null. (You will not receive a TypedValue whose type is TYPE_NULL.)

public void recycle ()

Added in API level 1

Recycle the TypedArray, to be re-used by a later caller. After calling this function you must not ever touch the typed array again.

public String toString ()

Added in API level 1

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
  • a printable representation of this object.