public class TypedArray extends Object
Resources.Theme.obtainStyledAttributes(AttributeSet, int[], int, int)
or Resources.obtainAttributes(android.util.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.Modifier and Type | Method and Description |
---|---|
int[] |
extractThemeAttrs()
Extracts theme attributes from a typed array for later resolution using
Resources.Theme.resolveAttributes(int[], int[]) . |
int[] |
extractThemeAttrs(int[] scrap) |
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.
|
ComplexColor |
getComplexColor(int index)
Retrieve the ComplexColor 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)
Retrieves a fractional unit attribute at index.
|
int |
getIndex(int at)
Returns 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,
int defValue)
Special version of
getDimensionPixelSize(int, int) for retrieving
ViewGroup 's layout_width and layout_height
attributes. |
int |
getLayoutDimension(int index,
String name)
Special version of
getDimensionPixelSize(int, int) for retrieving
ViewGroup 's layout_width and layout_height
attributes. |
String |
getNonConfigurationString(int index,
int allowedChangingConfigs)
Retrieves the string value for the attribute at index that is
not allowed to change with the given configurations.
|
String |
getNonResourceString(int index)
Retrieves 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)
Retrieves the resource identifier for the attribute at
index.
|
Resources |
getResources()
Returns the Resources object this array was loaded from.
|
String |
getString(int index)
Retrieves the string value for the attribute at index.
|
CharSequence |
getText(int index)
Retrieves the styled string value for the attribute at index.
|
CharSequence[] |
getTextArray(int index)
Retrieve the CharSequence[] for the attribute at index.
|
int |
getThemeAttributeId(int index,
int defValue)
Retrieves the theme attribute resource identifier 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()
Returns 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()
Recycles the TypedArray, to be re-used by a later caller.
|
String |
toString()
Returns a string representation of the object.
|
public int length()
RuntimeException
- if the TypedArray has already been recycled.public int getIndexCount()
RuntimeException
- if the TypedArray has already been recycled.public int getIndex(int at)
at
- The index you would like to returned, ranging from 0 to
getIndexCount()
.getValue(int, android.util.TypedValue)
and related APIs.RuntimeException
- if the TypedArray has already been recycled.public Resources getResources()
RuntimeException
- if the TypedArray has already been recycled.public CharSequence getText(@StyleableRes int index)
If the attribute is not a string, this method will attempt to coerce it to a string.
index
- Index of attribute to retrieve.null
if the attribute is not defined or could not be
coerced to a string.RuntimeException
- if the TypedArray has already been recycled.public String getString(@StyleableRes int index)
If the attribute is not a string, this method will attempt to coerce it to a string.
index
- Index of attribute to retrieve.null
if the attribute is not defined or could
not be coerced to a string.RuntimeException
- if the TypedArray has already been recycled.public String getNonResourceString(@StyleableRes int index)
index
- Index of attribute to retrieve.null
if the attribute is not defined or is not
an immediate string value.RuntimeException
- if the TypedArray has already been recycled.public String getNonConfigurationString(@StyleableRes int index, int allowedChangingConfigs)
index
- Index of attribute to retrieve.allowedChangingConfigs
- Bit mask of configurations from
Configuration
.NATIVE_CONFIG_* that are allowed to change.null
if the attribute is not defined.RuntimeException
- if the TypedArray has already been recycled.public boolean getBoolean(@StyleableRes int index, boolean defValue)
If the attribute is an integer value, this method will return whether
it is equal to zero. If the attribute is not a boolean or integer value,
this method will attempt to coerce it to an integer using
Integer.decode(String)
and return whether it is equal to zero.
index
- Index of attribute to retrieve.defValue
- Value to return if the attribute is not defined or
cannot be coerced to an integer.RuntimeException
- if the TypedArray has already been recycled.public int getInt(@StyleableRes int index, int defValue)
If the attribute is not an integer, this method will attempt to coerce
it to an integer using Integer.decode(String)
.
index
- Index of attribute to retrieve.defValue
- Value to return if the attribute is not defined or
cannot be coerced to an integer.RuntimeException
- if the TypedArray has already been recycled.public float getFloat(@StyleableRes int index, float defValue)
If the attribute is not a float or an integer, this method will attempt
to coerce it to a float using Float.parseFloat(String)
.
index
- Index of attribute to retrieve.RuntimeException
- if the TypedArray has already been recycled.public int getColor(@StyleableRes int index, int defValue)
ColorStateList
, then the default color from
the set is returned.
This method will throw an exception if the attribute is defined but is not an integer color or color state list.
index
- Index of attribute to retrieve.defValue
- Value to return if the attribute is not defined or
not a resource.RuntimeException
- if the TypedArray has already been recycled.UnsupportedOperationException
- if the attribute is defined but is
not an integer color or color state list.public ComplexColor getComplexColor(@StyleableRes int index)
ColorStateList
which can wrap a simple
color value or a GradientColor
This method will return null
if the attribute is not defined or
is not an integer color, color state list or GradientColor.
index
- Index of attribute to retrieve.null
if not defined.RuntimeException
- if the attribute if the TypedArray has already
been recycled.UnsupportedOperationException
- if the attribute is defined but is
not an integer color, color state list or GradientColor.public ColorStateList getColorStateList(@StyleableRes int index)
ColorStateList
description.
This method will return null
if the attribute is not defined or
is not an integer color or color state list.
index
- Index of attribute to retrieve.null
if not
defined.RuntimeException
- if the attribute if the TypedArray has already
been recycled.UnsupportedOperationException
- if the attribute is defined but is
not an integer color or color state list.public int getInteger(@StyleableRes int index, int defValue)
Unlike getInt(int, int)
, this method will throw an exception if
the attribute is defined but is not an integer.
index
- Index of attribute to retrieve.defValue
- Value to return if the attribute is not defined or
not a resource.RuntimeException
- if the TypedArray has already been recycled.UnsupportedOperationException
- if the attribute is defined but is
not an integer.public float getDimension(@StyleableRes int index, float defValue)
DisplayMetrics
associated with the resources this TypedArray
object
came from.
This method will throw an exception if the attribute is defined but is not a dimension.
index
- Index of attribute to retrieve.defValue
- Value to return if the attribute is not defined or
not a resource.RuntimeException
- if the TypedArray has already been recycled.UnsupportedOperationException
- if the attribute is defined but is
not an integer.getDimensionPixelOffset(int, int)
,
getDimensionPixelSize(int, int)
public int getDimensionPixelOffset(@StyleableRes int index, int defValue)
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.
This method will throw an exception if the attribute is defined but is not a dimension.
index
- Index of attribute to retrieve.defValue
- Value to return if the attribute is not defined or
not a resource.RuntimeException
- if the TypedArray has already been recycled.UnsupportedOperationException
- if the attribute is defined but is
not an integer.getDimension(int, float)
,
getDimensionPixelSize(int, int)
public int getDimensionPixelSize(@StyleableRes int index, int defValue)
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.
This method will throw an exception if the attribute is defined but is not a dimension.
index
- Index of attribute to retrieve.defValue
- Value to return if the attribute is not defined or
not a resource.RuntimeException
- if the TypedArray has already been recycled.UnsupportedOperationException
- if the attribute is defined but is
not a dimension.getDimension(int, float)
,
getDimensionPixelOffset(int, int)
public int getLayoutDimension(@StyleableRes int index, String name)
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)
.
This method will throw an exception if the attribute is defined but is not a dimension or integer (enum).
index
- Index of the attribute to retrieve.name
- Textual name of attribute for error reporting.RuntimeException
- if the TypedArray has already been recycled.UnsupportedOperationException
- if the attribute is defined but is
not a dimension or integer (enum).public int getLayoutDimension(@StyleableRes int index, int defValue)
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)
.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.RuntimeException
- if the TypedArray has already been recycled.public float getFraction(@StyleableRes int index, int base, int pbase, float defValue)
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.RuntimeException
- if the TypedArray has already been recycled.UnsupportedOperationException
- if the attribute is defined but is
not a fraction.@AnyRes public int getResourceId(@StyleableRes int index, int defValue)
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.index
- Index of attribute to retrieve.defValue
- Value to return if the attribute is not defined or
not a resource.RuntimeException
- if the TypedArray has already been recycled.public int getThemeAttributeId(@StyleableRes int index, int defValue)
index
- Index of attribute to retrieve.defValue
- Value to return if the attribute is not defined or not a
resource.RuntimeException
- if the TypedArray has already been recycled.public Drawable getDrawable(@StyleableRes int index)
This method will throw an exception if the attribute is defined but is not a color or drawable resource.
index
- Index of attribute to retrieve.null
if not defined.RuntimeException
- if the TypedArray has already been recycled.UnsupportedOperationException
- if the attribute is defined but is
not a color or drawable resource.public CharSequence[] getTextArray(@StyleableRes int index)
Resources.getTextArray
of the owning
Resources object to retrieve its String[].
This method will throw an exception if the attribute is defined but is not a text array resource.
index
- Index of attribute to retrieve.null
if not
defined.RuntimeException
- if the TypedArray has already been recycled.public boolean getValue(@StyleableRes int index, TypedValue outValue)
index
- Index of attribute to retrieve.outValue
- TypedValue object in which to place the attribute's
data.true
if the value was retrieved, false otherwise.RuntimeException
- if the TypedArray has already been recycled.public int getType(@StyleableRes int index)
index
- Index of attribute whose type to retrieve.RuntimeException
- if the TypedArray has already been recycled.public boolean hasValue(@StyleableRes int index)
Note: If the attribute was set to @empty
or
@undefined
, this method returns false
.
index
- Index of attribute to retrieve.RuntimeException
- if the TypedArray has already been recycled.public boolean hasValueOrEmpty(@StyleableRes int index)
true
if the attribute was explicitly set to @empty
and
false
only if the attribute was undefined.index
- Index of attribute to retrieve.RuntimeException
- if the TypedArray has already been recycled.public TypedValue peekValue(@StyleableRes int index)
TypedArray
.index
- Index of attribute to retrieve.RuntimeException
- if the TypedArray has already been recycled.public String getPositionDescription()
RuntimeException
- if the TypedArray has already been recycled.public void recycle()
RuntimeException
- if the TypedArray has already been recycled.public int[] extractThemeAttrs()
Resources.Theme.resolveAttributes(int[], int[])
.
Removes the entries from the typed array so that subsequent calls to typed
getters will return the default value without crashing.getIndexCount()
populated with theme
attributes, or null if there are no theme attributes in the typed
arrayRuntimeException
- if the TypedArray has already been recycled.public int[] extractThemeAttrs(int[] scrap)
public int getChangingConfigurations()
ActivityInfo
.RuntimeException
- if the TypedArray has already been recycled.ActivityInfo
public String toString()
Object
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())