TypedArray
open class TypedArray : AutoCloseable
Container for an array of values that were retrieved with Resources.Theme.obtainStyledAttributes(AttributeSet, int[], int, int)
or Resources.obtainAttributes
. 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 |
open Unit |
Recycles the TypedArray, to be re-used by a later caller.
|
open Boolean |
Retrieve the boolean value for the attribute at index.
|
open Int |
Return a mask of the configuration parameters for which the values in this typed array may change.
|
open Int |
Retrieve the color value for the attribute at index.
|
open ColorStateList? |
Retrieve the ColorStateList for the attribute at index.
|
open Float |
Retrieve a dimensional unit attribute at index.
|
open Int |
Retrieve a dimensional unit attribute at index for use as an offset in raw pixels.
|
open Int |
Retrieve a dimensional unit attribute at index for use as a size in raw pixels.
|
open Drawable? |
Retrieve the Drawable for the attribute at index.
|
open Float |
Retrieve the float value for the attribute at index.
|
open Typeface? |
Retrieve the Typeface for the attribute at index.
|
open Float |
Retrieves a fractional unit attribute at index.
|
open Int |
Returns an index in the array that has data.
|
open Int |
Returns the number of indices in the array that actually have data.
|
open Int |
Retrieve the integer value for the attribute at index.
|
open Int |
Retrieve the integer value for the attribute at index.
|
open Int |
Special version of getDimensionPixelSize for retrieving android.view.ViewGroup 's layout_width and layout_height attributes.
|
open Int |
Special version of getDimensionPixelSize for retrieving android.view.ViewGroup 's layout_width and layout_height attributes.
|
open String! |
Retrieves the string value for the attribute at index, but only if that string comes from an immediate value in an XML file.
|
open String! |
Returns a message about the parser state suitable for printing error messages.
|
open Int |
Retrieves the resource identifier for the attribute at index.
|
open Resources! |
Returns the Resources object this array was loaded from.
|
open Int |
Returns the resource ID of the style or layout against which the specified attribute was resolved, otherwise returns defValue.
|
open String? |
Retrieves the string value for the attribute at index.
|
open CharSequence! |
Retrieves the styled string value for the attribute at index.
|
open Array<CharSequence!>! |
Retrieve the CharSequence[] for the attribute at index.
|
open Int |
Returns the type of attribute at the specified index.
|
open Boolean |
Retrieve the raw TypedValue for the attribute at index.
|
open Boolean |
Determines whether there is an attribute at index.
|
open Boolean |
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.
|
open Int |
Returns the number of values in this array.
|
open TypedValue! |
Retrieve the raw TypedValue for the attribute at index and return a temporary object holding its data.
|
open Unit |
Recycles the TypedArray, to be re-used by a later caller.
|
open String |
|
Public methods
close
open fun close(): Unit
Recycles the TypedArray, to be re-used by a later caller. After calling this function you must not ever touch the typed array again.
Exceptions |
java.lang.Exception |
if this resource cannot be closed |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getBoolean
open fun getBoolean(
index: Int,
defValue: Boolean
): Boolean
Retrieve the boolean value for the attribute at index.
If the attribute is an integer value, this method returns false if the attribute is equal to zero, and true otherwise. 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.
Parameters |
index |
Int: Index of attribute to retrieve. |
defValue |
Boolean: Value to return if the attribute is not defined or cannot be coerced to an integer. |
Return |
Boolean |
Boolean value of the attribute, or defValue if the attribute was not defined or could not be coerced to an integer. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getChangingConfigurations
open fun getChangingConfigurations(): Int
Return a mask of the configuration parameters for which the values in this typed array may change.
Return |
Int |
Returns a mask of the changing configuration parameters, as defined by android.content.pm.ActivityInfo . Value is either 0 or a combination of android.content.pm.ActivityInfo#CONFIG_MCC , android.content.pm.ActivityInfo#CONFIG_MNC , android.content.pm.ActivityInfo#CONFIG_LOCALE , android.content.pm.ActivityInfo#CONFIG_TOUCHSCREEN , android.content.pm.ActivityInfo#CONFIG_KEYBOARD , android.content.pm.ActivityInfo#CONFIG_KEYBOARD_HIDDEN , android.content.pm.ActivityInfo#CONFIG_NAVIGATION , android.content.pm.ActivityInfo#CONFIG_ORIENTATION , android.content.pm.ActivityInfo#CONFIG_SCREEN_LAYOUT , android.content.pm.ActivityInfo#CONFIG_UI_MODE , android.content.pm.ActivityInfo#CONFIG_SCREEN_SIZE , android.content.pm.ActivityInfo#CONFIG_SMALLEST_SCREEN_SIZE , android.content.pm.ActivityInfo#CONFIG_DENSITY , android.content.pm.ActivityInfo#CONFIG_LAYOUT_DIRECTION , android.content.pm.ActivityInfo#CONFIG_COLOR_MODE , android.content.pm.ActivityInfo#CONFIG_FONT_SCALE , android.content.pm.ActivityInfo#CONFIG_GRAMMATICAL_GENDER , android.content.pm.ActivityInfo#CONFIG_ASSETS_PATHS , and android.content.pm.ActivityInfo#CONFIG_RESOURCES_UNUSED |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getColor
open fun getColor(
index: Int,
defValue: Int
): Int
Retrieve the color value for the attribute at index. If the attribute references a color resource holding a complex android.content.res.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.
Parameters |
index |
Int: Index of attribute to retrieve. |
defValue |
Int: Value to return if the attribute is not defined or not a resource. |
Return |
Int |
Attribute color value, or defValue if not defined. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
java.lang.UnsupportedOperationException |
if the attribute is defined but is not an integer color or color state list. |
getColorStateList
open fun getColorStateList(index: Int): ColorStateList?
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 android.content.res.ColorStateList
description.
This method will return null
if the attribute is not defined or is not an integer color or color state list.
Parameters |
index |
Int: Index of attribute to retrieve. |
Return |
ColorStateList? |
ColorStateList for the attribute, or null if not defined. |
Exceptions |
java.lang.RuntimeException |
if the attribute if the TypedArray has already been recycled. |
java.lang.UnsupportedOperationException |
if the attribute is defined but is not an integer color or color state list. |
getDimension
open fun getDimension(
index: Int,
defValue: Float
): Float
Retrieve a dimensional unit attribute at index. Unit conversions are based on the current 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.
Parameters |
index |
Int: Index of attribute to retrieve. |
defValue |
Float: Value to return if the attribute is not defined or not a resource. |
Return |
Float |
Attribute dimension value multiplied by the appropriate metric, or defValue if not defined. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
java.lang.UnsupportedOperationException |
if the attribute is defined but is not an integer. |
getDimensionPixelOffset
open fun getDimensionPixelOffset(
index: Int,
defValue: Int
): Int
Retrieve a dimensional unit attribute at index for use as an offset in raw pixels. This is the same as getDimension
, 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.
Parameters |
index |
Int: Index of attribute to retrieve. |
defValue |
Int: Value to return if the attribute is not defined or not a resource. |
Return |
Int |
Attribute dimension value multiplied by the appropriate metric and truncated to integer pixels, or defValue if not defined. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
java.lang.UnsupportedOperationException |
if the attribute is defined but is not an integer. |
getDimensionPixelSize
open fun getDimensionPixelSize(
index: Int,
defValue: Int
): Int
Retrieve a dimensional unit attribute at index for use as a size in raw pixels. This is the same as getDimension
, 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.
Parameters |
index |
Int: Index of attribute to retrieve. |
defValue |
Int: Value to return if the attribute is not defined or not a resource. |
Return |
Int |
Attribute dimension value multiplied by the appropriate metric and truncated to integer pixels, or defValue if not defined. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
java.lang.UnsupportedOperationException |
if the attribute is defined but is not a dimension. |
getDrawable
open fun getDrawable(index: Int): Drawable?
Retrieve the Drawable for the attribute at index.
This method will throw an exception if the attribute is defined but is not a color or drawable resource.
Parameters |
index |
Int: Index of attribute to retrieve. |
Return |
Drawable? |
Drawable for the attribute, or null if not defined. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
java.lang.UnsupportedOperationException |
if the attribute is defined but is not a color or drawable resource. |
getFloat
open fun getFloat(
index: Int,
defValue: Float
): Float
Retrieve the float value for the attribute at index.
If the attribute is not a float or an integer, this method will attempt to coerce it to a float using Float.parseFloat(String)
.
Parameters |
index |
Int: Index of attribute to retrieve. |
Return |
Float |
Attribute float value, or defValue if the attribute was not defined or could not be coerced to a float. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getFont
open fun getFont(index: Int): Typeface?
Retrieve the Typeface for the attribute at index.
This method will throw an exception if the attribute is defined but is not a font.
Parameters |
index |
Int: Index of attribute to retrieve. |
Return |
Typeface? |
Typeface for the attribute, or null if not defined. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
java.lang.UnsupportedOperationException |
if the attribute is defined but is not a font resource. |
getFraction
open fun getFraction(
index: Int,
base: Int,
pbase: Int,
defValue: Float
): Float
Retrieves a fractional unit attribute at index.
Parameters |
index |
Int: Index of attribute to retrieve. |
base |
Int: The base value of this fraction. In other words, a standard fraction is multiplied by this value. |
pbase |
Int: The parent base value of this fraction. In other words, a parent fraction (nn%p) is multiplied by this value. |
defValue |
Float: Value to return if the attribute is not defined or not a resource. |
Return |
Float |
Attribute fractional value multiplied by the appropriate base value, or defValue if not defined. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
java.lang.UnsupportedOperationException |
if the attribute is defined but is not a fraction. |
getIndex
open fun getIndex(at: Int): Int
Returns an index in the array that has data. Attributes with a value of @empty are included, as this is an explicit indicator.
Parameters |
at |
Int: The index you would like to returned, ranging from 0 to getIndexCount() . |
Return |
Int |
The index at the given offset, which can be used with getValue and related APIs. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getIndexCount
open fun getIndexCount(): Int
Returns the number of indices in the array that actually have data. Attributes with a value of @empty are included, as this is an explicit indicator.
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getInt
open fun getInt(
index: Int,
defValue: Int
): Int
Retrieve the integer value for the attribute at index.
If the attribute is not an integer, this method will attempt to coerce it to an integer using Integer.decode(String)
.
Parameters |
index |
Int: Index of attribute to retrieve. |
defValue |
Int: Value to return if the attribute is not defined or cannot be coerced to an integer. |
Return |
Int |
Integer value of the attribute, or defValue if the attribute was not defined or could not be coerced to an integer. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getInteger
open fun getInteger(
index: Int,
defValue: Int
): Int
Retrieve the integer value for the attribute at index.
Unlike getInt(int,int)
, this method will throw an exception if the attribute is defined but is not an integer.
Parameters |
index |
Int: Index of attribute to retrieve. |
defValue |
Int: Value to return if the attribute is not defined or not a resource. |
Return |
Int |
Attribute integer value, or defValue if not defined. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
java.lang.UnsupportedOperationException |
if the attribute is defined but is not an integer. |
getLayoutDimension
open fun getLayoutDimension(
index: Int,
defValue: Int
): Int
Special version of getDimensionPixelSize
for retrieving android.view.ViewGroup
's layout_width and layout_height attributes. This is only here for performance reasons; applications should use getDimensionPixelSize
.
Parameters |
index |
Int: Index of the attribute to retrieve. |
defValue |
Int: The default value to return if this attribute is not default or contains the wrong type of data. |
Return |
Int |
Attribute dimension value multiplied by the appropriate metric and truncated to integer pixels. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getLayoutDimension
open fun getLayoutDimension(
index: Int,
name: String!
): Int
Special version of getDimensionPixelSize
for retrieving android.view.ViewGroup
's layout_width and layout_height attributes. This is only here for performance reasons; applications should use getDimensionPixelSize
.
This method will throw an exception if the attribute is defined but is not a dimension or integer (enum).
Parameters |
index |
Int: Index of the attribute to retrieve. |
name |
String!: Textual name of attribute for error reporting. |
Return |
Int |
Attribute dimension value multiplied by the appropriate metric and truncated to integer pixels. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
java.lang.UnsupportedOperationException |
if the attribute is defined but is not a dimension or integer (enum). |
getNonResourceString
open fun getNonResourceString(index: Int): String!
Retrieves 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 |
Int: Index of attribute to retrieve. |
Return |
String! |
String holding string data. Any styling information is removed. Returns null if the attribute is not defined or is not an immediate string value. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getPositionDescription
open fun getPositionDescription(): String!
Returns a message about the parser state suitable for printing error messages.
Return |
String! |
Human-readable description of current parser state. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getResourceId
open fun getResourceId(
index: Int,
defValue: Int
): Int
Retrieves 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 |
Int: Index of attribute to retrieve. |
defValue |
Int: Value to return if the attribute is not defined or not a resource. |
Return |
Int |
Attribute resource identifier, or defValue if not defined. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getResources
open fun getResources(): Resources!
Returns the Resources object this array was loaded from.
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getSourceResourceId
open fun getSourceResourceId(
index: Int,
defaultValue: Int
): Int
Returns the resource ID of the style or layout against which the specified attribute was resolved, otherwise returns defValue. For example, if you we resolving two attributes android:attribute1
and android:attribute2
and you were inflating a android.view.View
from layout/my_layout.xml
:
<View
style="@style/viewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:attribute1="foo"/>
and
@style/viewStyle
is:
<style android:name="viewStyle">
<item name="android:attribute2">bar<item/>
<style/>
then resolved
TypedArray
will have values that return source resource ID of
R.layout.my_layout
for
android:attribute1
and
R.style.viewStyle
for
android:attribute2
.
Parameters |
index |
Int: Index of attribute whose source style to retrieve. |
defaultValue |
Int: Value to return if the attribute is not defined or not a resource. |
Return |
Int |
Either a style resource ID, layout resource ID, or defaultValue if it was not resolved in a style or layout. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getString
open fun getString(index: Int): String?
Retrieves the string value for the attribute at index.
If the attribute is not a string, this method will attempt to coerce it to a string.
Parameters |
index |
Int: Index of attribute to retrieve. |
Return |
String? |
String holding string data. Any styling information is removed. Returns null if the attribute is not defined or could not be coerced to a string. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getText
open fun getText(index: Int): CharSequence!
Retrieves the styled string value for the attribute at index.
If the attribute is not a string, this method will attempt to coerce it to a string.
Parameters |
index |
Int: Index of attribute to retrieve. |
Return |
CharSequence! |
CharSequence holding string data. May be styled. Returns null if the attribute is not defined or could not be coerced to a string. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getTextArray
open fun getTextArray(index: Int): Array<CharSequence!>!
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[].
This method will throw an exception if the attribute is defined but is not a text array resource.
Parameters |
index |
Int: Index of attribute to retrieve. |
Return |
Array<CharSequence!>! |
CharSequence[] for the attribute, or null if not defined. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getType
open fun getType(index: Int): Int
Returns the type of attribute at the specified index.
Parameters |
index |
Int: Index of attribute whose type to retrieve. |
Return |
Int |
Attribute type. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
getValue
open fun getValue(
index: Int,
outValue: TypedValue!
): Boolean
Retrieve the raw TypedValue for the attribute at index.
Parameters |
index |
Int: Index of attribute to retrieve. |
outValue |
TypedValue!: TypedValue object in which to place the attribute's data. |
Return |
Boolean |
true if the value was retrieved and not @empty, false otherwise. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
hasValue
open fun hasValue(index: Int): Boolean
Determines whether there is an attribute at index.
Note: If the attribute was set to @empty
or @undefined
, this method returns false
.
Parameters |
index |
Int: Index of attribute to retrieve. |
Return |
Boolean |
True if the attribute has a value, false otherwise. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
hasValueOrEmpty
open fun hasValueOrEmpty(index: Int): Boolean
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 |
Int: Index of attribute to retrieve. |
Return |
Boolean |
True if the attribute has a value or is empty, false otherwise. |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
length
open fun length(): Int
Returns the number of values in this array.
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
peekValue
open fun peekValue(index: Int): TypedValue!
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 |
Int: Index of attribute to retrieve. |
Return |
TypedValue! |
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.) |
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
recycle
open fun recycle(): Unit
Recycles the TypedArray, to be re-used by a later caller. After calling this function you must not ever touch the typed array again.
Exceptions |
java.lang.RuntimeException |
if the TypedArray has already been recycled. |
toString
open fun toString(): String
Return |
String |
a string representation of the object. |