Class TypedArray

java.lang.Object
icyllis.modernui.resources.TypedArray

public class TypedArray extends Object
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    TypedArray(Resources resources)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    getBoolean(int index, boolean defValue)
    Retrieve the boolean value for the attribute at index.
    int
    getColor(int index, int defValue)
    Retrieve the color value for the attribute at index.
    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.
    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
    Returns 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.
    getResourceId(int index)
    Retrieves the resource identifier for the attribute at index.
    Returns the Resources object this array was loaded from.
    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
    Returns the number of values in this array.
    peekValue(int index)
    Retrieve the raw TypedValue for the attribute at index and return a temporary object holding its data.
    void
    Recycles the TypedArray, to be re-used by a later caller.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TypedArray

      protected TypedArray(Resources resources)
  • Method Details

    • length

      public int length()
      Returns the number of values in this array.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • getIndexCount

      public int getIndexCount()
      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.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • getIndex

      public int getIndex(int at)
      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 - The index you would like to returned, ranging from 0 to getIndexCount().
      Returns:
      The index at the given offset, which can be used with getValue(int, icyllis.modernui.resources.TypedValue) and related APIs.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • getResources

      public Resources getResources()
      Returns the Resources object this array was loaded from.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • getBoolean

      public boolean getBoolean(@StyleableRes int index, boolean defValue)
      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 - Index of attribute to retrieve.
      defValue - Value to return if the attribute is not defined or cannot be coerced to an integer.
      Returns:
      Boolean value of the attribute, or defValue if the attribute was not defined or could not be coerced to an integer.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • getInt

      public int getInt(@StyleableRes int index, int defValue)
      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 - Index of attribute to retrieve.
      defValue - Value to return if the attribute is not defined or cannot be coerced to an integer.
      Returns:
      Integer value of the attribute, or defValue if the attribute was not defined or could not be coerced to an integer.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • getFloat

      public float getFloat(@StyleableRes int index, float defValue)
      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 - Index of attribute to retrieve.
      Returns:
      Attribute float value, or defValue if the attribute was not defined or could not be coerced to a float.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • getColor

      @ColorInt public int getColor(@StyleableRes int index, @ColorInt int defValue)
      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.

      This method will throw an exception if the attribute is defined but is not an integer color or color state list.

      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.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
      UnsupportedOperationException - if the attribute is defined but is not an integer color or color state list.
    • getColorStateList

      @Nullable public ColorStateList getColorStateList(@StyleableRes int index)
      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.

      This method will return null if the attribute is not defined or is not an integer color or color state list.

      Parameters:
      index - Index of attribute to retrieve.
      Returns:
      ColorStateList for the attribute, or null if not defined.
      Throws:
      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.
    • getInteger

      public int getInteger(@StyleableRes int index, int defValue)
      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 - 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.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
      UnsupportedOperationException - if the attribute is defined but is not an integer.
    • getDimension

      public float getDimension(@StyleableRes int index, float defValue)
      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 - 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.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
      UnsupportedOperationException - if the attribute is defined but is not an integer.
      See Also:
    • getDimensionPixelOffset

      public int getDimensionPixelOffset(@StyleableRes int index, int defValue)
      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.

      This method will throw an exception if the attribute is defined but is not a dimension.

      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.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
      UnsupportedOperationException - if the attribute is defined but is not an integer.
      See Also:
    • getDimensionPixelSize

      public int getDimensionPixelSize(@StyleableRes int index, int defValue)
      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.

      This method will throw an exception if the attribute is defined but is not a dimension.

      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.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
      UnsupportedOperationException - if the attribute is defined but is not a dimension.
      See Also:
    • getLayoutDimension

      public int getLayoutDimension(@StyleableRes int index, int defValue)
      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.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • getFraction

      public float getFraction(@StyleableRes int index, int base, int pbase, float defValue)
      Retrieves 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.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
      UnsupportedOperationException - if the attribute is defined but is not a fraction.
    • getResourceId

      @Nullable @AnyRes public ResourceId getResourceId(@StyleableRes int index)
      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 - Index of attribute to retrieve.
      Returns:
      Attribute resource identifier, or defValue if not defined.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • getDrawable

      @Nullable public Drawable getDrawable(@StyleableRes int index)
      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 - Index of attribute to retrieve.
      Returns:
      Drawable for the attribute, or null if not defined.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
      UnsupportedOperationException - if the attribute is defined but is not a color or drawable resource.
    • getValue

      public boolean getValue(@StyleableRes int index, TypedValue outValue)
      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:
      true if the value was retrieved and not @empty, false otherwise.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • getType

      public int getType(@StyleableRes int index)
      Returns the type of attribute at the specified index.
      Parameters:
      index - Index of attribute whose type to retrieve.
      Returns:
      Attribute type.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • hasValue

      public boolean hasValue(@StyleableRes int index)
      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.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • hasValueOrEmpty

      public boolean hasValueOrEmpty(@StyleableRes 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.
      Parameters:
      index - Index of attribute to retrieve.
      Returns:
      True if the attribute has a value or is empty, false otherwise.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • peekValue

      @Nullable public TypedValue peekValue(@StyleableRes int index)
      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.)
      Throws:
      RuntimeException - if the TypedArray has already been recycled.
    • recycle

      public void recycle()
      Recycles the TypedArray, to be re-used by a later caller. After calling this function you must not ever touch the typed array again.
      Throws:
      RuntimeException - if the TypedArray has already been recycled.