23.2.0 set vector drawable as background in 4.X
Asked Answered
C

1

17

I am super excited about the new possibility to set vector drawables to layouts using the app:srcCompat="@drawable/icon" attribute introduced in support-library 23.2.0.

But I wonder how I can fetch one of this drawables programmatically or set is as background.

I thought about something like: ContextCompat.getDrawable(context, R.drawable.icon)

Is this even possible?

Cellarage answered 10/3, 2016 at 12:2 Comment(2)
I never tried so I'm just guessing, but it seems that there's a static method on VectorDrawableCompat called createFromResource(Resource, id); that should work. android.googlesource.com/platform/frameworks/support/+/34cbdb2/…Bettinabettine
Doesn't seem to be there with 23.2.0 in Android Studio.Cellarage
C
38

Okay, I managed it myself. Thanks for @Budius for pointing me in the right direction.

The Answers lays in VectorDrawable#create()

Resources resources = context.getResources(Resources, int, Theme);
Theme theme = context.getTheme();
Drawable drawable = VectorDrawableCompat.create(resources, R.drawable.drawable, theme);

More input: How to use vector drawables in Android API lower 21?

Cellarage answered 10/3, 2016 at 13:49 Comment(1)
There is not a xml way like app:srcCompat for view to use VectorDrawable as background. The only way to use VectorDrawable as a background pre android lollipop may be VectorDrawableCompat, thanks for share.Selfinsurance

© 2022 - 2024 — McMap. All rights reserved.