Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 it's 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?

like image 215
Neeraj Avatar asked Feb 26 '16 06:02

Neeraj


2 Answers

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.

like image 194
Bhavesh Patadiya Avatar answered Sep 20 '22 13:09

Bhavesh Patadiya


Combining many answers into one answer:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:background="@color/primary">       <android.support.v7.widget.AppCompatImageView         android:layout_width="300dp"         android:layout_height="100dp"         android:id="@+id/logoImageView"         android:layout_centerHorizontal="true"         android:layout_centerVertical="true"         app:srcCompat="@drawable/my_logo_vector"         android:tint="@color/white"         />  </RelativeLayout> 

This worked for me. No lint errors. Use AppCompatImageView.

like image 29
Micro Avatar answered Sep 20 '22 13:09

Micro