Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get a Drawable object via a TypedArray when the drawable resource is a Vector Drawable?

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?

like image 430
TomTaila Avatar asked Mar 17 '17 16:03

TomTaila


People also ask

What is a vector drawable?

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.

Which of the following method of the drawable class allow the client to interact with the drawn object?

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.


1 Answers

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.

like image 85
TomTaila Avatar answered Oct 09 '22 15:10

TomTaila