I'm trying to use a VectorDrawable
on API21, but Android loads the PNG resource from xxhdpi
folder instead.
My current res
structure as follows:
And my XML layout:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/test_icon"/>
Are there any other ways to solve this? From my understanding Android will always pick the PNG resource, but if that's the case, how one can use VectorDrawables
for API21
and PNG for lower API's?
[Update 1]
If we use a drawable-xxhdpi-21
resource folder, Android will pick the vector instead of the PNG resource. But that means we would have to have a copy (or symlink) of the file for other densities as well (e.g. xhdpi
, hdpi
, etc)
drawable-anydpi-v21 is right for that
drawable-anydpi-v21/test_icon.xml
https://google.github.io/material-design-icons/#icons-for-android
The solution I found is a bit of a hack but seems to work. Instead of putting your VertorDrawable
xml files in each of the bucket with the version, if you put them in drawable-w1dp-v21
it takes the right resource and don't need to have multiple copies.
This is because, when deciding which resource to use, Android checks the density and after the minimum width density, which in this case is always gonna be more than 1dp :)
Update: as @pengrad mentions best to use the anydpi
bucket. :)
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