What is AttributeSet and how can i use it?

AndroidAttributesView

Android Problem Overview


What is AttributeSet in Android?

How can i use it for my custom view?

Android Solutions


Solution 1 - Android

A late answer, although a detailed description, for others.

AttributeSet (Android Docs) > A collection of attributes, as found associated with a tag in an XML document.

Basically if you are trying to create a custom view, and you want to pass in values like dimensions, colors etc, you can do so with AttributeSet.

Here's an example

Imagine you want to create a View like below

enter image description here

There's a rectangle with yellow background, and a circle inside it, with let's say 5dp radius, and green background. If you want your Views to take the values of background colors and radius through XML, like this:

<com.anjithsasindran.RectangleView
    app:radiusDimen="5dp"
    app:rectangleBackground="@color/yellow"
    app:circleBackground="@color/green" />

Well that's where AttributeSet is used. You can have this file attrs.xml in values folder, with the following properties.

<declare-styleable name="RectangleViewAttrs">
    <attr name="rectangle_background" format="color" />
    <attr name="circle_background" format="color" />
    <attr name="radius_dimen" format="dimension" />
</declare-styleable>

Since this is a View, the java class extends from View

public class RectangleView extends View {
    
    public RectangleView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.RectangleViewAttrs);
        mRadiusHeight = attributes.getDimensionPixelSize(R.styleable.RectangleViewAttrs_radius_dimen, getDimensionInPixel(50));
        mCircleBackgroundColor = attributes.getDimensionPixelSize(R.styleable.RectangleViewAttrs_circle_background, getDimensionInPixel(20));
        mRectangleBackgroundColor = attributes.getColor(R.styleable.RectangleViewAttrs_rectangle_background, Color.BLACK);
        attributes.recycle()
    }
}

So now we can use, these properties to our RectangleView in your xml layout, and we will obtain these values in the RectangleView constructor.

app:radius_dimen
app:circle_background
app:rectangle_background

Solution 2 - Android

You can use AttributeSet to get extra, custom values for your view which you define in the xml. For example. There's a tutorial about Defining Custom Attributes which states, "it's possible to read values from the AttributeSet directly" but it doesn't say how to actually do this. It does warn, however, that if you don't use styled attributes then:

  • Resource references within attribute values are not resolved
  • Styles are not applied

If you want to ignore the whole styled attributes thing and just get the attributes directly:

example.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:custom="http://www.chooseanything.org">

  <com.example.CustomTextView
    android:text="Blah blah blah"
    custom:myvalue="I like cheese"/>

</LinearLayout>

Note there are two lines of xmlns (xmlns = XML namespace), the second is defined as xmlns:custom. Then below that custom:myvalue is defined.

CustomTextView.java

public CustomTextView( Context context, AttributeSet attrs )
{
  super( context, attrs );
  String sMyValue = attrs.getAttributeValue( "http://www.chooseanything.org", "myvalue" );
  // Do something useful with sMyValue
}

Solution 3 - Android

AttributeSet is the set of properties specified in an xml resource file. You shouldn't have to do anything special in your custom view. The View(Context context, AttributeSet attrs) gets called to initialize a view from a layout file. Just add this constructor to your custom view. Check out the Custom View example in the SDK to see it used.

Solution 4 - Android

When a view is created from an XML layout, all of the attributes in XML tag are read from the resource bundle and passed to view's constructor as an AttributeSet

Although it is possible to read values from the AttributeSetdirectly, doing so has some disadvantages:

  • Resource reference within attribute values are not resolved
  • Styles are not applied

Instead pass AttributeSet to obtainStyledAttribute(). This method passes back TypedArray array of values that have been deferenced and styled.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPremierView Question on Stackoverflow
Solution 1 - Androidcapt.swagView Answer on Stackoverflow
Solution 2 - AndroidDaveView Answer on Stackoverflow
Solution 3 - AndroidRobby PondView Answer on Stackoverflow
Solution 4 - AndroidDaksh GargasView Answer on Stackoverflow