4

enter image description here I added an ImageButton to my CardView, and when I add app:srcComapat to my ImageButton. I'm getting an error like below:

Error:(27) No resource identifier found for attribute 'srcCompat' in package 'com.example.jaisonjoseph.newsclient'

here is my content_main.xml layout:

<?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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.jaisonjoseph.newsclient.MainActivity"
    tools:showIn="@layout/app_bar_main"
    style="@style/Base.Widget.AppCompat.ButtonBar"
    android:background="#ffffff">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="350dp"
        android:layout_height="150dp"
        app:cardCornerRadius="6dp"
        android:background="#f6f6f6"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/daily"
            android:id="@+id/imageButton3"
            android:src="@drawable/daily" />
    </android.support.v7.widget.CardView>
</RelativeLayout>
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Jaison_Joseph
  • 333
  • 7
  • 26

2 Answers2

3

Just change RelativeLayout property

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

to

xmlns:app="http://schemas.android.com/apk/lib/com.example.jaisonjoseph.newsclient"

Edit :

add this

xmlns:app="http://schemas.android.com/apk/lib/com.example.jaisonjoseph.newsclient"

For more detail visit this : No resource identifier found for attribute ' ' in package 'com.app....'

Complete Code :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/lib/com.example.jaisonjoseph.newsclient"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.jaisonjoseph.newsclient.MainActivity"
        tools:showIn="@layout/app_bar_main"
        style="@style/Base.Widget.AppCompat.ButtonBar"
        android:background="#ffffff">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="350dp"
        android:layout_height="150dp"
        app:cardCornerRadius="6dp"
        android:background="#f6f6f6"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/daily"
            android:id="@+id/imageButton3"
            android:src="@drawable/daily" />
    </android.support.v7.widget.CardView>
</RelativeLayout>
Community
  • 1
  • 1
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
2

what you are getting it seems like it's just a lint error that can be ignored.

you can use:

tools:ignore="MissingPrefix"

in your RelativeLayout to avoid seeing this error temporarily.

Because srcCompat attribute is actually defined within AppCompat library, so remember to add AppCompat library to your project.


UPDATE
For another reader how landing in this thread.

Do not use app's namespace using project name like:

xmlns:app="http://schemas.android.com/apk/res/com.yourproject.name"

Instead use:

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

As it is said in https://code.google.com/p/android/issues/detail?id=9656#c71:

"Added support for custom views with custom attributes in libraries. Layouts using custom attributes must use the namespace URI schemas.android.com/apk/res-auto instead of the URI that includes the app package name. This URI is replaced with the app specific one at build time."

Read more at https://code.google.com/p/android/issues/detail?id=9656

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96