Search code examples
androidandroid-vectordrawable

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?


Solution

  • 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.