Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VectorDrawable: Android loads xhdpi PNG's instead of the vector resource

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:

  • res
    • drawable-xxhdpi
      • test_icon.png
    • drawable-21
      • test_icon.xml

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)

like image 262
Mokkun Avatar asked Mar 12 '15 03:03

Mokkun


2 Answers

drawable-anydpi-v21 is right for that

drawable-anydpi-v21/test_icon.xml

https://google.github.io/material-design-icons/#icons-for-android

like image 172
Stas Parshin Avatar answered Nov 11 '22 12:11

Stas Parshin


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. :)

like image 38
pablisco Avatar answered Nov 11 '22 12:11

pablisco