public interface

AttributeSet

android.util.AttributeSet
Known Indirect Subclasses

Class Overview

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.getAttributeSet(). 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(myResouce);
 AttributeSet attributes = Xml.getAttributeSet(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(String namespace, String attribute, boolean defaultValue)
Return the boolean value of 'attribute'.
abstract boolean getAttributeBooleanValue(int index, boolean defaultValue)
Return the boolean value of attribute at 'index'.
abstract int getAttributeCount()
abstract float getAttributeFloatValue(int index, float defaultValue)
Return the float value of attribute at 'index'.
abstract float getAttributeFloatValue(String namespace, String attribute, float defaultValue)
Return the float value of 'attribute'.
abstract int getAttributeIntValue(int index, int defaultValue)
Return the integer value of attribute at 'index'.
abstract int getAttributeIntValue(String namespace, String attribute, int defaultValue)
Return the integer value of 'attribute'.
abstract int getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue)
Return the index of the value of 'attribute' in the list 'options'.
abstract int getAttributeListValue(int index, String[] options, int defaultValue)
Return the index of the value of attribute at 'index' in the list 'options'.
abstract String getAttributeName(int index)
abstract int getAttributeNameResource(int index)
Return the resource ID associated with the given attribute name.
abstract int getAttributeResourceValue(String namespace, String attribute, int defaultValue)
Return the value of 'attribute' as a resource identifier.
abstract int getAttributeResourceValue(int index, int defaultValue)
Return the value of attribute at 'index' as a resource identifier.
abstract int getAttributeUnsignedIntValue(String namespace, String attribute, int defaultValue)
Return the boolean value of 'attribute' that is formatted as an unsigned value.
abstract int getAttributeUnsignedIntValue(int index, int defaultValue)
Return the integer value of attribute at 'index' that is formatted as an unsigned value.
abstract String getAttributeValue(String namespace, String name)
abstract String getAttributeValue(int index)
abstract String getClassAttribute()
Return the value of the "class" attribute or null if there is not one.
abstract String getIdAttribute()
Return the value of the "id" attribute or null if there is not one.
abstract int getIdAttributeResourceValue(int defaultValue)
Return the integer value of the "id" attribute or defaultValue if there is none.
abstract String getPositionDescription()
abstract int getStyleAttribute()
Return the value of the "style" attribute or 0 if there is not one.

Public Methods

public abstract boolean getAttributeBooleanValue (String namespace, String attribute, boolean defaultValue)

Return the boolean value of 'attribute'.

Parameters
attribute The attribute to retrieve.
defaultValue What to return if the attribute isn't found.
Returns
  • Resulting value.

public abstract boolean getAttributeBooleanValue (int index, boolean defaultValue)

Return the boolean value of attribute at 'index'.

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

public abstract int getAttributeCount ()

public abstract float getAttributeFloatValue (int index, float defaultValue)

Return the float value of attribute at 'index'.

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

public abstract float getAttributeFloatValue (String namespace, String attribute, float defaultValue)

Return the float value of 'attribute'.

Parameters
attribute The attribute to retrieve.
defaultValue What to return if the attribute isn't found.
Returns
  • Resulting value.

public abstract int getAttributeIntValue (int index, int defaultValue)

Return the integer value of attribute at 'index'.

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

public abstract int getAttributeIntValue (String namespace, String attribute, int defaultValue)

Return the integer value of 'attribute'.

Parameters
attribute The attribute to retrieve.
defaultValue What to return if the attribute isn't found.
Returns
  • Resulting value.

public abstract int getAttributeListValue (String namespace, String attribute, String[] options, int defaultValue)

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

Parameters
attribute Name of attribute to retrieve.
options List of strings whose values we are checking against.
defaultValue Value returned if attribute doesn't exist or no match is found.
Returns
  • Index in to 'options' or defaultValue.

public abstract int getAttributeListValue (int index, String[] options, int defaultValue)

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

Parameters
index Index of the desired attribute, 0...count-1.
options List of strings whose values we are checking against.
defaultValue Value returned if attribute doesn't exist or no match is found.
Returns
  • Index in to 'options' or defaultValue.

public abstract String getAttributeName (int index)

public abstract int getAttributeNameResource (int index)

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(int, int) 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 Index of the desired attribute, 0...count-1.
Returns
  • The resource identifier, 0 if none.

public abstract int getAttributeResourceValue (String namespace, String attribute, int defaultValue)

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

Note that this is different than getAttributeNameResource(int) in that it returns a 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
attribute The attribute to retrieve.
defaultValue What to return if the attribute isn't found.
Returns
  • Resulting value.

public abstract int getAttributeResourceValue (int index, int defaultValue)

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

Note that this is different than getAttributeNameResource(int) in that it returns a 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 Index of the desired attribute, 0...count-1.
defaultValue What to return if the attribute isn't found.
Returns
  • Resulting value.

public abstract int getAttributeUnsignedIntValue (String namespace, String attribute, int defaultValue)

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
attribute The attribute to retrieve.
defaultValue What to return if the attribute isn't found.
Returns
  • Resulting value.

public abstract int getAttributeUnsignedIntValue (int index, int defaultValue)

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 Index of the desired attribute, 0...count-1.
defaultValue What to return if the attribute isn't found.
Returns
  • Resulting value.

public abstract String getAttributeValue (String namespace, String name)

public abstract String getAttributeValue (int index)

public abstract String getClassAttribute ()

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

Returns
  • The class attribute's value or null.

public abstract String getIdAttribute ()

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

Returns
  • The id attribute's value or null.

public abstract int getIdAttributeResourceValue (int defaultValue)

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

Parameters
defaultValue What to return if the "id" attribute isn't found.
Returns
  • int Resulting value.

public abstract String getPositionDescription ()

public abstract int getStyleAttribute ()

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

Returns
  • The style attribute's resource identifier or 0.