1

I'd like to start using vectors for the icons with in the apps I do, but not sure how to deal with the compatibility

I'm not interested in any of the third party compat libraries and happy to wait for googles version (Mentioned here VectorDrawable - is it available somehow for pre-Lollipop versions of Android?).

So basically for now I'm happy to continue to have the images per resolution as well as an additional for > Lollipop. If the device is higher than 21 it will use the vector otherwise it will fall back to the standard png's.

I can have the vector in a drawable-v21 folder, but the drawable-[dpi] folder takes precedence over the version, meaning the vector isn't used.

I'm hoping the vector is used so that when the compat comes available I can simply delete all the png's and know the vectors will be ok as I have been able to test them on a device running Lollipop.

ankuranurag2
  • 2,300
  • 15
  • 30
Stimsoni
  • 3,166
  • 2
  • 29
  • 22

6 Answers6

2

Now google released Android Support Library 23.2 Support Vector Drawables and Animated Vector Drawables !

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

Note:

- Vector images all the way back to API 7 (Android 2.1 Eclair).

- Animated vectors are a bit more limited, going only as far back as API 11

LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
2

In order to have backwards compatibility for Vector Drawables here is the following configuration I used for API Level pre-20 devices.

build.gradle

vectorDrawables.useSupportLibrary = true

dependencies{classpath 'com.android.tools.build:gradle:2.1.2'}

Activity

onCreate > AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

XML ImageView

app:srcCompat="@drawable/vector"

Programmatic ImageView

imageView.setBackgroundResource(R.vector);

vector.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="30dp"
        android:height="20dp"
        android:viewportHeight="20.0"
        android:viewportWidth="30.0">
    <path
        android:fillColor="#4C5F70"
        android:pathData="DATA_HERE"
        android:strokeColor="#00000000"
        android:strokeWidth="1"/>
</vector>
AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
1

From Android 5.0 (API level 21) you can use vector drawable in your app. You can use new Android Studio tool called: Vector Asset Studio. It handles PNG making for older versions automatically. See link below for a complete explanation:

Vector Asset Studio

1

With Android Support Library 23.2, VectorDrawables are now supported from API level 7 onwards and AnimatedVectorDrawables from API level 11 so.

So now you can use

app:srcCompat

instead of

app:src

for using VectorDrawables with backward compatibility.

Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
darshanz
  • 402
  • 3
  • 11
  • Nice update. Do you have a link to the details around this. Has google put up any details regarding it. – Stimsoni Feb 25 '16 at 04:47
1

if you use SRC, then use this:

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

You don't need to add that line in every activity, if you added it once in the Application class it will work as well. Like this:

public class App extends Application {

static {

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

REMEMBER: You still need to have enabled the use of the support library in app level gradle:

android {

  defaultConfig {

    vectorDrawables.useSupportLibrary = true

  }

}

Inside xml use:

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

When you want to use vector drawable as drawableTop, bottom, right ,left then used LayerDrawable otherwise it will crash on devices lower than 5.0

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/search"/>
</level-list>
Darthcow
  • 358
  • 4
  • 14
Rahil Ali
  • 957
  • 10
  • 25
0

EDIT: This was an interim fix until Android studio 1.4 was released. There is no longer a need for this. See Ka Developer's answer instead

Found a solution that while not brilliant does work.

You need to create a lollypop drawables folder for each of the dpi folders and add the same vector to each folder.

Eg create the folders

drawable-hdpi-v21
drawable-xhdpi-v21
drawable-xxhdpi-v21
drawable-xxxhdpi-v21

Stimsoni
  • 3,166
  • 2
  • 29
  • 22