Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use VectorDrawables in Android API lower than 21?

I am working on an Android project and I chose <vector> to display icon because it is adaptable and dynamically, however, I just can run this app on devices running Android, which have API 21 or higher. My question is how can I use <vector> on lower Android version i.e. API 14 or kind of. Thanks!

<!-- drawable/ic_android_debug_bridge.xml --> <vector xmlns:android="http://schemas.android.com/apk/res/android"     android:height="48dp"     android:width="48dp"     android:viewportWidth="24"     android:viewportHeight="24">     <path android:fillColor="@color/primaryColorDark"         android:pathData="M15,9A1,1 0 0,1 14,8A1,1 0 0,1 15,7A1,1 0 0,1 16,8A1,1 `0 0,1 15,9M9,9A1,1 0 0,1 8,8A1,1 0 0,1 9,7A1,1 0 0,1 10,8A1,1 0 0,1 9,9M16.12,4.37L18.22,2.27L17.4,1.44L15.09,3.75C14.16,3.28 13.11,3 12,3C10.88,3 9.84,3.28 8.91,3.75L6.6,1.44L5.78,2.27L7.88,4.37C6.14,5.64 5,7.68 5,10V11H19V10C19,7.68 17.86,5.64 16.12,4.37M5,16C5,19.86 8.13,23 12,23A7,7 0 0,0 19,16V12H5V16Z" /></vector> 
like image 389
iamatsundere181 Avatar asked Dec 22 '15 14:12

iamatsundere181


People also ask

How do I change the color of a vector asset?

Don't edit the vector assets directly. If you're using a vector drawable in an ImageButton, just choose your color in android:tint . android:tint works on all android versions since APIv1. What you mean is drawableTint.

Does Android studio support SVG?

Android Studio includes a tool called Vector Asset Studio that helps you add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files into your project as vector drawable resources.


2 Answers

With the support library 23.2, the true support for Vector Drawables has been provided all the way down to API v7. It is recommended to disable the previous version of the support, which rendered PNG during build-time, by adding

// Gradle Plugin 2.0+  android {    defaultConfig {      vectorDrawables.useSupportLibrary = true     }  } 

to the build.gradle file.

The implementation is fairly simple. Just use the new srcCompat attribute on Drawables (under app namespace!):

<ImageView   android:layout_width="wrap_content"   android:layout_height="wrap_content"   app:srcCompat="@drawable/ic_add" />    // <= this is new 

Vector Drawables are also supported in cases like TextView's drawableLeft property.

Source: library announcement

However, I would still recommend something like Iconics library, AndroidSVG, or another font-icon or SVG solution for the full SVG-standand vector support.

like image 59
Alen Siljak Avatar answered Sep 25 '22 19:09

Alen Siljak


VectorDrawable are supported pre-Lollipop via the Support Library, but the way to use them depends on the version of Support Library you have. And it may not work in all cases.

I've made this diagram to help (valid for Support Library 23.4.0 to - at least - 25.1.0).

VectorDrawable cheatsheet

like image 30
David Ferrand Avatar answered Sep 22 '22 19:09

David Ferrand