Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid drawable tag vector

Im trying to use vector drawables on pre lollipop devices. I did all as instructed here but i still get this crash.

build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-beta6'
    }
}
apply plugin: 'com.android.application'

repositories {
    maven { url 'http://maven.android-forever.com' }
    jcenter()
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.test.app"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:design:23.2.0'
    compile "de.greenrobot:eventbus:2.4.0"
    compile 'de.greenrobot:greendao:2.1.0'
    compile "com.af:android-utility:1.0.0.9"
    compile project(':volleyplus')
    compile project (':libvlc')
}

triangle.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/triangle_v"/>
</selector>

triangle_v.xml

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

<path
    android:name="triangle"
    android:fillColor="#FF0000"
    android:pathData="m 50,0 l 50,100 -100,0 z"/>

</vector>

layout.xml

<ImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/triangle"/>

I also tried app:srcCompat and in that case, drawable just dont show.

like image 568
pedja Avatar asked Mar 05 '16 20:03

pedja


5 Answers

This code is going to work with vector if using
vectorDrawables.useSupportLibrary = true

And change android:src to app:srcCompat.

For example,

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/triangle"/>

to

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:srcCompat="@drawable/triangle"/>
like image 92
алекс кей Avatar answered Oct 14 '22 17:10

алекс кей


Got this problem too when loading vectors from a selector on pre-lollipop devices:

Use AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) in your onCreate method:

Sets whether vector drawables on older platforms (< API 21) can be used within android.graphics.drawable.DrawableContainer resources. When enabled, AppCompat can intercept some drawable inflation from the framework, which enables implicit inflation of vector drawables within android.graphics.drawable.DrawableContainer resources.

protected final void onCreate(Bundle savedInstanceState) {
         AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
         super.onCreate(savedInstanceState);
         ...
like image 35
Aldasa Avatar answered Oct 14 '22 17:10

Aldasa


I faced a similar problem and @pedja's own answer is useful. More generally, as mentioned in Chris Banes's article on vector drawable compat, the support library works by injecting its version of ImageView over the system one on pre-L via some hooks. This implicitly requires the AppCompat versions of classes, such as AppCompatActivity, be used.

In my case, the vector drawable is used in a standalone toast-like view without an associated activity, using the Application context. I ended up using AppCompatImageView in the xml layout definition directly, i.e. something like

<android.support.v7.widget.AppCompatImageView
             android:id="@+id/some_id"
             android:layout_width="24dp"
             android:layout_height="24dp"
             android:src="@drawable/selector_referencing_vector_drawable"/>

thus there is no need for the magic "hook" mechanism. As tested this also works with the Activity class without the need of using AppCompatActivity. All the above was done without upgrading to 23.2.1, which addressed a different problem.

like image 21
headuck Avatar answered Oct 14 '22 17:10

headuck


The problem was that my activity wasn't extending AppCompatActivity but regular Activity.

This is not specified in any documentation/example for support vector drawables

like image 19
pedja Avatar answered Oct 14 '22 16:10

pedja


The answers given here are ignoring a situation when you wish to add a drawable to a textview because it gives the same error. in my case I had

<TextView .... android:drawableLeft="some_vectore_drawable" />

I could not find how to solve this so I removed that line from the xml code and put it in my java code in this manner

Drawable somevectordrable = AppCompatDrawableManager.get().getDrawable(context, R.drawable.somevectordrawable);
mytextview.setCompoundDrawableWithIntrinsicBounds(somevectordrable, null, null, null);

Clarification for the code,

  1. Get the vector drawable from the drawables folder using AppCompatDrawableManager

  2. Set the drawable we just got as the left drawable on our textview

like image 14
nada Avatar answered Oct 14 '22 15:10

nada