Added in API level 1

AttributeSet


interface AttributeSet
android.util.AttributeSet

A collection of attributes, as found associated with a tag in an XML document. Often you will not want to use this interface directly, instead passing it to Resources.Theme.obtainStyledAttributes() which will take care of parsing the attributes for you. In particular, the Resources API will convert resource references (attribute values such as "@string/my_label" in the original XML) to the desired type for you; if you use AttributeSet directly then you will need to manually check for resource references (with getAttributeResourceValue(int,int)) and do the resource lookup yourself if needed. Direct use of AttributeSet also prevents the application of themes and styles when retrieving attribute values.

This interface provides an efficient mechanism for retrieving data from compiled XML files, which can be retrieved for a particular XmlPullParser through Xml.asAttributeSet(). Normally this will return an implementation of the interface that works on top of a generic XmlPullParser, however it is more useful in conjunction with compiled XML resources:

XmlPullParser parser = resources.getXml(myResource);
  AttributeSet attributes = Xml.asAttributeSet(parser);

The implementation returned here, unlike using the implementation on top of a generic XmlPullParser, is highly optimized by retrieving pre-computed information that was generated by aapt when compiling your resources. For example, the getAttributeFloatValue(int,float) method returns a floating point number previous stored in the compiled resource instead of parsing at runtime the string originally in the XML file.

This interface also provides additional information contained in the compiled XML resource that is not available in a normal XML file, such as getAttributeNameResource(int) which returns the resource identifier associated with a particular XML attribute name.

Summary

Public methods
abstract Boolean
getAttributeBooleanValue(namespace: String!, attribute: String!, defaultValue: Boolean)

Return the boolean value of 'attribute'.

abstract Boolean
getAttributeBooleanValue(index: Int, defaultValue: Boolean)

Return the boolean value of attribute at 'index'.

abstract Int

Returns the number of attributes available in the set.

abstract Float
getAttributeFloatValue(namespace: String!, attribute: String!, defaultValue: Float)

Return the float value of 'attribute'.

abstract Float
getAttributeFloatValue(index: Int, defaultValue: Float)

Return the float value of attribute at 'index'.

abstract Int
getAttributeIntValue(namespace: String!, attribute: String!, defaultValue: Int)

Return the integer value of 'attribute'.

abstract Int
getAttributeIntValue(index: Int, defaultValue: Int)

Return the integer value of attribute at 'index'.

abstract Int
getAttributeListValue(namespace: String!, attribute: String!, options: Array<String!>!, defaultValue: Int)

Return the index of the value of 'attribute' in the list 'options'.

abstract Int
getAttributeListValue(index: Int, options: Array<String!>!, defaultValue: Int)

Return the index of the value of attribute at 'index' in the list 'options'.

abstract String!

Returns the name of the specified attribute.

abstract Int

Return the resource ID associated with the given attribute name.

open String!

Returns the namespace of the specified attribute.

abstract Int
getAttributeResourceValue(namespace: String!, attribute: String!, defaultValue: Int)

Return the value of 'attribute' as a resource identifier.

abstract Int
getAttributeResourceValue(index: Int, defaultValue: Int)

Return the value of attribute at 'index' as a resource identifier.

abstract Int
getAttributeUnsignedIntValue(namespace: String!, attribute: String!, defaultValue: Int)

Return the boolean value of 'attribute' that is formatted as an unsigned value.

abstract Int
getAttributeUnsignedIntValue(index: Int, defaultValue: Int)

Return the integer value of attribute at 'index' that is formatted as an unsigned value.

abstract String!

Returns the value of the specified attribute as a string representation.

abstract String!
getAttributeValue(namespace: String!, name: String!)

Returns the value of the specified attribute as a string representation.

abstract String!

Return the value of the "class" attribute or null if there is not one.

abstract String!

Return the value of the "id" attribute or null if there is not one.

abstract Int

Return the integer value of the "id" attribute or defaultValue if there is none.

abstract String!

Returns a description of the current position of the attribute set.

abstract Int

Return the value of the "style" attribute or 0 if there is not one.

Public methods

getAttributeBooleanValue

Added in API level 1
abstract fun getAttributeBooleanValue(
    namespace: String!,
    attribute: String!,
    defaultValue: Boolean
): Boolean

Return the boolean value of 'attribute'.

Parameters
namespace String!: Namespace of attribute to retrieve.
attribute String!: The attribute to retrieve.
defaultValue Boolean: What to return if the attribute isn't found.
Return
Boolean Resulting value.

getAttributeBooleanValue

Added in API level 1
abstract fun getAttributeBooleanValue(
    index: Int,
    defaultValue: Boolean
): Boolean

Return the boolean value of attribute at 'index'.

Parameters
index Int: Index of the desired attribute, 0...count-1.
defaultValue Boolean: What to return if the attribute isn't found.
Return
Boolean Resulting value.

getAttributeCount

Added in API level 1
abstract fun getAttributeCount(): Int

Returns the number of attributes available in the set.

See also XmlPullParser.getAttributeCount(), which this method corresponds to when parsing a compiled XML file.

Return
Int A positive integer, or 0 if the set is empty.

getAttributeFloatValue

Added in API level 1
abstract fun getAttributeFloatValue(
    namespace: String!,
    attribute: String!,
    defaultValue: Float
): Float

Return the float value of 'attribute'.

Parameters
namespace String!: Namespace of attribute to retrieve.
attribute String!: The attribute to retrieve.
defaultValue Float: What to return if the attribute isn't found.
Return
Float Resulting value.

getAttributeFloatValue

Added in API level 1
abstract fun getAttributeFloatValue(
    index: Int,
    defaultValue: Float
): Float

Return the float value of attribute at 'index'.

Parameters
index Int: Index of the desired attribute, 0...count-1.
defaultValue Float: What to return if the attribute isn't found.
Return
Float Resulting value.

getAttributeIntValue

Added in API level 1
abstract fun getAttributeIntValue(
    namespace: String!,
    attribute: String!,
    defaultValue: Int
): Int

Return the integer value of 'attribute'.

Parameters
namespace String!: Namespace of attribute to retrieve.
attribute String!: The attribute to retrieve.
defaultValue Int: What to return if the attribute isn't found.
Return
Int Resulting value.

getAttributeIntValue

Added in API level 1
abstract fun getAttributeIntValue(
    index: Int,
    defaultValue: Int
): Int

Return the integer value of attribute at 'index'.

Parameters
index Int: Index of the desired attribute, 0...count-1.
defaultValue Int: What to return if the attribute isn't found.
Return
Int Resulting value.

getAttributeListValue

Added in API level 1
abstract fun getAttributeListValue(
    namespace: String!,
    attribute: String!,
    options: Array<String!>!,
    defaultValue: Int
): Int

Return the index of the value of 'attribute' in the list 'options'.

Parameters
namespace String!: Namespace of attribute to retrieve.
attribute String!: Name of attribute to retrieve.
options Array<String!>!: List of strings whose values we are checking against.
defaultValue Int: Value returned if attribute doesn't exist or no match is found.
Return
Int Index in to 'options' or defaultValue.

getAttributeListValue

Added in API level 1
abstract fun getAttributeListValue(
    index: Int,
    options: Array<String!>!,
    defaultValue: Int
): Int

Return the index of the value of attribute at 'index' in the list 'options'.

Parameters
index Int: Index of the desired attribute, 0...count-1.
options Array<String!>!: List of strings whose values we are checking against.
defaultValue Int: Value returned if attribute doesn't exist or no match is found.
Return
Int Index in to 'options' or defaultValue.

getAttributeName

Added in API level 1
abstract fun getAttributeName(index: Int): String!

Returns the name of the specified attribute.

See also XmlPullParser.getAttributeName(), which this method corresponds to when parsing a compiled XML file.

Parameters
index Int: Index of the desired attribute, 0...count-1.
Return
String! A String containing the name of the attribute, or null if the attribute cannot be found.

getAttributeNameResource

Added in API level 1
abstract fun getAttributeNameResource(index: Int): Int

Return the resource ID associated with the given attribute name. This will be the identifier for an attribute resource, which can be used by styles. Returns 0 if there is no resource associated with this attribute.

Note that this is different than #getAttributeResourceValue in that it returns a resource identifier for the attribute name; the other method returns this attribute's value as a resource identifier.

Parameters
index Int: Index of the desired attribute, 0...count-1.
Return
Int The resource identifier, 0 if none.

getAttributeNamespace

Added in API level 28
open fun getAttributeNamespace(index: Int): String!

Returns the namespace of the specified attribute.

See also XmlPullParser.getAttributeNamespace(), which this method corresponds to when parsing a compiled XML file.

Parameters
index Int: Index of the desired attribute, 0...count-1.
Return
String! A String containing the namespace of the attribute, or null if th attribute cannot be found.

getAttributeResourceValue

Added in API level 1
abstract fun getAttributeResourceValue(
    namespace: String!,
    attribute: String!,
    defaultValue: Int
): Int

Return the value of 'attribute' as a resource identifier.

Note that this is different than getAttributeNameResource in that it returns the value contained in this attribute as a resource identifier (i.e., a value originally of the form "@package:type/resource"); the other method returns a resource identifier that identifies the name of the attribute.

Parameters
namespace String!: Namespace of attribute to retrieve.
attribute String!: The attribute to retrieve.
defaultValue Int: What to return if the attribute isn't found.
Return
Int Resulting value.

getAttributeResourceValue

Added in API level 1
abstract fun getAttributeResourceValue(
    index: Int,
    defaultValue: Int
): Int

Return the value of attribute at 'index' as a resource identifier.

Note that this is different than getAttributeNameResource in that it returns the value contained in this attribute as a resource identifier (i.e., a value originally of the form "@package:type/resource"); the other method returns a resource identifier that identifies the name of the attribute.

Parameters
index Int: Index of the desired attribute, 0...count-1.
defaultValue Int: What to return if the attribute isn't found.
Return
Int Resulting value.

getAttributeUnsignedIntValue

Added in API level 1
abstract fun getAttributeUnsignedIntValue(
    namespace: String!,
    attribute: String!,
    defaultValue: Int
): Int

Return the boolean value of 'attribute' that is formatted as an unsigned value. In particular, the formats 0xn...n and #n...n are handled.

Parameters
namespace String!: Namespace of attribute to retrieve.
attribute String!: The attribute to retrieve.
defaultValue Int: What to return if the attribute isn't found.
Return
Int Resulting value.

getAttributeUnsignedIntValue

Added in API level 1
abstract fun getAttributeUnsignedIntValue(
    index: Int,
    defaultValue: Int
): Int

Return the integer value of attribute at 'index' that is formatted as an unsigned value. In particular, the formats 0xn...n and #n...n are handled.

Parameters
index Int: Index of the desired attribute, 0...count-1.
defaultValue Int: What to return if the attribute isn't found.
Return
Int Resulting value.

getAttributeValue

Added in API level 1
abstract fun getAttributeValue(index: Int): String!

Returns the value of the specified attribute as a string representation.

Parameters
index Int: Index of the desired attribute, 0...count-1.
Return
String! A String containing the value of the attribute, or null if the attribute cannot be found.

getAttributeValue

Added in API level 1
abstract fun getAttributeValue(
    namespace: String!,
    name: String!
): String!

Returns the value of the specified attribute as a string representation. The lookup is performed using the attribute name.

Parameters
namespace String!: The namespace of the attribute to get the value from.
name String!: The name of the attribute to get the value from.
Return
String! A String containing the value of the attribute, or null if the attribute cannot be found.

getClassAttribute

Added in API level 1
abstract fun getClassAttribute(): String!

Return the value of the "class" attribute or null if there is not one. Equivalent to getAttributeValue(null, "class").

Return
String! The class attribute's value or null.

getIdAttribute

Added in API level 1
abstract fun getIdAttribute(): String!

Return the value of the "id" attribute or null if there is not one. Equivalent to getAttributeValue(null, "id").

Return
String! The id attribute's value or null.

getIdAttributeResourceValue

Added in API level 1
abstract fun getIdAttributeResourceValue(defaultValue: Int): Int

Return the integer value of the "id" attribute or defaultValue if there is none. Equivalent to getAttributeResourceValue(null, "id", defaultValue);

Parameters
defaultValue Int: What to return if the "id" attribute isn't found.
Return
Int int Resulting value.

getPositionDescription

Added in API level 1
abstract fun getPositionDescription(): String!

Returns a description of the current position of the attribute set. For instance, if the attribute set is loaded from an XML document, the position description could indicate the current line number.

Return
String! A string representation of the current position in the set, may be null.

getStyleAttribute

Added in API level 1
abstract fun getStyleAttribute(): Int

Return the value of the "style" attribute or 0 if there is not one. Equivalent to getAttributeResourceValue(null, "style").

Return
Int The style attribute's resource identifier or 0.