Background color from the TypedArray

895 Views Asked by At

I have a custom view in Android and want to load all attributes set on the custom view by the the attrs and the TypedArray. I define for instance a backcolor property in the xml layout but cannot load it in the constructor.

I did an attempt but without success.

// To be done, get the background color from the attributes.
TypedArray l_typedArray = this.getContext().obtainStyledAttributes(attrs,??                                , 0);
// not working Color backColor = l_typedArray.getColor()
1

There are 1 best solutions below

0
jdekeij On

The attributes you can read here are in the attrs.xml in the values\attrs.xml - Can look as follows

<resources>
<declare-styleable name="bubbleview">
    <!-- Color of the playground  -->
    <attr name="playgroundcolor" format="reference|color" />
    <!-- Color of the canon arrow coming out of the top ball  -->
    <attr name="canoncolor" format="reference|color" />
    <!-- Color of the score text  -->
    <attr name="scorecolor" format="reference|color"/>
</declare-styleable>
</resources>





enter code here
{
TypedArray l_typedArray = 
getContext().obtainStyledAttributes(attrs,R.styleable.bubbleview);
m_scorecolor = l_typedArray.getColor(R.styleable.bubbleview_scorecolor,Color.RED);
m_canoncolor = l_typedArray.getColor(R.styleable.bubbleview_canoncolor,Color.BLUE);
l_typedArray.recycle(); // never forget this one!!!
}