Neeraj Neeraj - 1 month ago 11
Android Question

Can't use srcCompat for ImageViews in android

I'm using the Design Support Library 23.2.
I've added these lines in my build.gradle as my Gradle Plugin is version 1.5

defaultConfig {
applicationId "com.abc.xyz"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
generatedDensities = []
}

aaptOptions {
additionalParameters "--no-version-vectors"
}
}


as its specified in here

But i can't use the srcCompat attribute for my imageview.

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


where @drawable/wallpaper is a vector resource file

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M4,4h7V2H4c-1.1,0 -2,0.9 -2,2v7h2V4zm6,9l-4,5h12l-3,-4 -2.03,2.71L10,13zm7,-4.5c0,-0.83 -0.67,-1.5 -1.5,-1.5S14,7.67 14,8.5s0.67,1.5 1.5,1.5S17,9.33 17,8.5zM20,2h-7v2h7v7h2V4c0,-1.1 -0.9,-2 -2,-2zm0,18h-7v2h7c1.1,0 2,-0.9 2,-2v-7h-2v7zM4,13H2v7c0,1.1 0.9,2 2,2h7v-2H4v-7z"/>




It says


Error:(14) No resource identifier found for attribute 'srcCompat' in
package 'android'


My gradle version is 1.5. How can i use srcCompat.

Answer

Don't

android:srcCompat="@drawable/wallpaper"

DO

app:srcCompat="@drawable/wallpaper"

as it srcCompat attribute is actually defined within AppCompat library.

Important you will need to add appropriate namespace for this.

xmlns:app="http://schemas.android.com/apk/res-auto"

Important

what you are getting it seems like it is just a lint error that can be ignored. I have tried and have the same error, but it is working correctly.

you can use tools:ignore="MissingPrefix" to avoid seeing this error temporarily.

I hope this helps.

Comments