I have written a custom compound view with custom attributes. One of the custom attributes is a drawable and the file I wish to use is a Vector Drawable.
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0)
val iconDrawable = typedArray.getDrawable(R.styleable.CustomView_icon_drawable)
I keep getting a XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
Why is this?
A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information. The major advantage of using a vector drawable is image scalability.
In addition to simple drawing, Drawable provides a number of generic mechanisms for its client to interact with what is being drawn: The setBounds(Rect) method must be called to tell the Drawable where it is drawn and how large it should be.
Solved.
I needed to do the following:
val drawableResId = typedArray.getResourceId(R.styleable.CustomView_icon_drawable, -1);
val drawable = AppCompatResources.getDrawable(getContext(), drawableResId)
Credit to pskink and creck for the solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With