Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android XML drawables scaling

I'm struggling to understand the usage pattern behind XML drawables. Some sources say that they should automatically scale based on resolution. My experience says different. For example, using absolute size in a vector like this:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp" ... />

Will actually display this shape in size 24dp in all resolutions. I do not observe any automatic scaling happening at all, e.g. it looks tiny on tablets resolutions.

So I declared various sizes in values/dimens.xml, and added it to the shape like this:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="@dimen/status_icon_size"
        android:height="@dimen/status_icon_size" ... />

which sort of fixed the original issue, but then it fails to build after adding min-sdk = 19 (Android 4.4). When generating bitmaps from these vectors the compiler seems to have problem using values from dimens.xml:

Error:Error: Width (0) and height (0) cannot be <= 0

I want to avoid having multiple icon.xml in all possible drawables_w***dp like with dimens.xml. I already have a bunch of dimens.xml. Isn't the whole purpose of vector icons to have easy scaleability?

Q: Is there a way to make a properly scalable icon which works for all screen resolutions with just one instance of the XML file in /res/drawable?

like image 251
Califf Avatar asked Mar 07 '16 18:03

Califf


1 Answers

To avoid this error Error:Error: Width (0) and height (0) cannot be <= 0 Add this string to app build.gradle:

defaultConfig {
  ...
  vectorDrawables.useSupportLibrary = true
}
like image 160
Alex Avatar answered Oct 27 '22 11:10

Alex