Im trying to use vector drawables on pre lollipop devices. I did all as instructed here but i still get this crash.
build.gradle:
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.
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"/>
That's all I fond over the internet, and it's work for me. Check which one you miss.
1) Inside build.gradle
android {
...
defaultConfig {
...
vectorDrawables.useSupportLibrary = true
}
}
2) Must use buildToolsVersion '27.0.3'
and compile 'com.android.support:appcompat-v7:27.0.3'
similar version code.
3) Use upper then 3 gradle version
classpath 'com.android.tools.build:gradle:3.0.1'
4) For ImageView
use app:srcCompat
xmlns:app="http://schemas.android.com/apk/res-auto"
app:srcCompat="@drawable/ic_logo"
5) But if you need android:drawableLeft
or android:drawableRigth
etc then:
ic_logo.xml (Vector xml)
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:width="@dimen/home.button.icon"
android:height="@dimen/home.button.icon"
android:viewportWidth="24.0"
android:viewportHeight="24.0"
tools:ignore="VectorRaster">
<path
android:fillColor="#FFFFFF"
android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z"/>
</vector>
ic_logo_select.xml (Vector selector)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/ic_logo" />
<item android:drawable="@drawable/ic_logo" />
</selector>
Now you can use android:drawableLeft or android:drawableRight
<android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_logo_select"/>
6) And at the end, setCompatVectorFromResourcesEnabled = true inside static which loads before the main method.
public class MainActivity extends AppCompatActivity {
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
}
}
I found this issue on code.google.
It appears to be the same issue as you. Does this help? https://code.google.com/p/android/issues/detail?id=201843
It works with 23.2.0 or 23.4.0, not 23.3.0. Seriously Google!
First up, this functionality was originally released in 23.2.0, but then we found some memory usage and Configuration updating issues so we it removed in 23.3.0. In 23.4.0 (technically a fix release) we’ve re-added the same functionality but behind a flag which you need to manually enable.
Ref : https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.waicp19gh
You also need to include the new vector support library:
compile 'com.android.support:support-vector-drawable:23.2.0'