Android StateListDrawable from svg-s below API level 21

345 Views Asked by At

I would like to refactor my app using scalable vector graphics before API level 21 also (It is possible since support library 23.2, which is a cool staff), instead of generating png-s. My only problem is, that I cannot make it work with StateListDrawables. Before the refactor I have used StateListDrawables like:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/dashboard_reset_max_alt_disabled" />
    <item android:state_pressed="true" android:drawable="@drawable/dashboard_reset_max_alt_pressed"/>
    <item android:drawable="@drawable/dashboard_reset_max_alt"/>
</selector>

where "dashboard_reset_max_alt*" are svg-s.

But if I set the "vectorDrawables.useSupportLibrary = true" in my build.gradle I cannot use them neither in app:srcCompat attribute nor in getDrawable() method. Do you know a solution to use the xml files I have already and svg-s also?

The crash log:

android.content.res.Resources$NotFoundException: File res/drawable/computer_cog_20dp.xml from drawable resource ID #0x7f02016c
at android.content.res.Resources.loadDrawable(Resources.java:3063)
at android.content.res.Resources.getDrawable(Resources.java:1624)
at com.myapp.helper.SkinHelper.getDrawable(SkinHelper.java:426)
...

The getDrawable method looks like:

Drawable d;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    d = c.getDrawable(resId);
else d = c.getResources().getDrawable(resId);
// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
return DrawableCompat.wrap(d);

I know that I can create StateListDrawables programmatically, but I would prefer a workaround so I can use my xml-s that I have already. Do you have any ideas?

0

There are 0 best solutions below