55
<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/card_outer_padding"
    android:layout_marginTop="@dimen/card_outer_padding"
    android:layout_marginRight="@dimen/card_outer_padding"
    android:layout_marginBottom='@{model.cardBottomMargin}'
    android:foreground="?attr/selectableItemBackground"
    android:onClick="@{model::onCardClick}"
    app:cardElevation="2dp"
    app:cardCornerRadius="2dp"
    app:cardUseCompatPadding="true">
</android.support.v7.widget.CardView>

I got this error message

Cannot resolve symbol ?attr/selectableItemBackground Validates resource references inside Android XML files.

<TextView
    android:id="@+id/country_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="40dp"
    android:text="@{model.name}"
    style="@style/TextAppearance.AppCompat.Headline"
    tools:text="Country"/>

And, I got a similar error for the above as well

Cannot resolve symbol '@style/TextAppearance.AppCompat.Headline'
Validates resource references inside Android XML files.

Any pointer would be great! Thank you! It seems to be related to this thread but no solution was provided:

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
Jun
  • 2,942
  • 5
  • 28
  • 50

10 Answers10

143

I believe this is a communication problem with the IDE and Android Gradle Plugin. In any case, the only way I've reliably found to resolve this is to delete the problematic libs in the .idea/libraries/ folder.

So, for you, since the lint checks aren't recognizing ?attr/selectableItemBackground

  1. LOCATE the Gradle__com_android_support_xxx.xml files
  2. DELETE those files
  3. SYNC IDE with file system
  4. SYNC project with Gradle files
  5. REBUILD your project

You can sync by clicking on 'File' at the top by the toolbar and selecting "Sync with File System" and then afterwards selecting "Sync project with gradle files".

I've tried other suggested solutions - had the appcompat-v7 dependency in my module build.gradle file & the google() repository as the first line in the project build.gradle dependencies. Nada.

These solutions also haven't helped...

  • Invalidating the caches/restarting didn't work.
  • Cleaning/Rebuilding the project.
wooldridgetm
  • 2,500
  • 1
  • 16
  • 22
  • 8
    I have similar problem with `?attr/colorPrimary`, but since deleting `Gradle__com_android_support_xxx.xml` where xxx is the version number is not working for me, I just delete all files that start with `Gradle__com_android_support_` and it works. – HendraWD Apr 20 '18 at 05:32
  • After many attempts this was a unique one that solved me, thanks for sharing. Thanks – Marco Giovanni Apr 21 '18 at 15:23
  • 2
    I didnt able to find Gradle__com_android_support_xxx.xml – Dheeraj Rijhwani Apr 30 '18 at 13:34
  • 1
    Didn't helped... eh... I've tried almost everything:) – Demigod May 07 '18 at 11:42
  • You on Canary (3.2) or Production (3.1)? – wooldridgetm May 07 '18 at 15:07
  • 1
    Did not help at all on Ubuntu, also tried HendraWD's solution. – jeff May 14 '18 at 10:55
  • Worked here like a spell! – X09 May 27 '18 at 13:24
  • Solved for me,Android Studio 3.1.3 Build #AI-173.4819257, built on June 4, 2018 JRE: 1.8.0_152-release-1024-b02 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Windows 10 10.0 – djleop Jun 21 '18 at 08:37
  • helped me, thanks! i tried the other solutions but nothing worked – mapo Jul 16 '18 at 16:07
  • This definitely helped me. Lost a few days trying to resolve this until i found this solution – Scrappy Jul 16 '18 at 17:32
  • This helped me. In the end I just deleted the entire library folder in the .idea folder to make sure I didn't forget deleting any specific file, and restarted using Invalidate/Restart. Building took twice as long as usual, but the issue is gone. – Wout Jul 24 '18 at 09:10
43

For version Lollipop and higher use this:

android:foreground="?android:attr/selectableItemBackground"

for Pre-Lollipop use this:

android:foreground="?attr/selectableItemBackground"
J.D.
  • 109
  • 1
  • 11
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
  • 7
    This is not the correct solution most of the times. I ask you to please explain to the readers what the difference is and why one may work and the other may not, as well as what the implications of each choice is. If you know this, then you can’t be possibly telling people that in this case, the solution is to prepend “android” to an attribute. – Martin Marconcini Jun 04 '18 at 21:04
  • 2
    This is not the best solution, since it will force the foreground to be whatever the platform specifies it as. So, on Android 4.0, this will be a holo themed `selectableItemBackground`. Most people want to use the resource that AppCompat defines, which requires you to remove the `android` prefix. – Jawnnypoo Jun 20 '18 at 22:05
  • 1
    Dear Downvoter and @MartinMarconcini as i already mentioned in my previous edit that i have just tried and its worked for me, if you know the exact reason or any one else in SO, feel free to edit and add the desired information, i would appreciate it. – Vishal Yadav Jul 17 '18 at 18:31
  • 1
    I don’t know who downvoted this, but I reiterate: this is not the solution to the proposed problem. This is the same as fixing a null pointer exception with `if (x != null)`. The problem lies deep within the way themes/styles/attributes work on Android and the clusterfuck of solutions Google tries to explain with blog posts every now and then. Do all Android Developers easily understand the difference between themes, styles, attributes and how to apply each, and also how they look in different platform versions? I bet the answer is no. Heck even I don’t know all the differences. – Martin Marconcini Jul 17 '18 at 19:47
  • Yes, the OP can’t find a style, but by telling him to use “android” instead, I believe (and hence why I left a comment) that you should try (if you know/can) why to explain why this works, why are there two versions and, in your experience, what the side-effects one must be aware of. Just that. If this works for your project, then Im all for it, but your project theme/API levels are unknown to me as a reader, so this may not apply. – Martin Marconcini Jul 17 '18 at 19:49
  • And lastly, I hope you understand where I come from, but the OP’s problem was a missing attribute problem, since the pre-lollipop version is w/out the attribute, even if you’re in a post-lollipop world, there’s no way the non-android-prefixed version cannot be found unless there is another problem. Your solution is correct to specify different values/layouts for different APIs, but with other side-effects and things to keep in mind. (App Compat overlay themes and how you apply the theme/style, depending which platform you are running, etc.). So you see, there’s a lot more than meet the eye. – Martin Marconcini Jul 17 '18 at 19:53
9

2 options:

Option 1

Another possible reason is: Google's maven repository is not set for the build script.

Open your project's main build.gradle add this line:

buildscript {
    repositories {
        google()  <-- this
        // Be also sure that google() is before jcenter()
    }
}

Without this, it may not be able to download the Android Studio Gradle plugin 3.0+. It's not distributed in jCenter but in the Google's maven repository.

Option 2

Run this command in root of project and resync project

Linux:

rm .idea/libraries/Gradle__com_android_support_*.xml

Windows:

del .idea\libraries\Gradle__com_android_support_*.xml

HerberthObregon
  • 1,811
  • 19
  • 23
4

I faced with same error after updating Kotlin. Solved with "Invalidate caches/Restart"


UPDATE Today this solution did not helped me. But the solution of this question did: Android Studio 3.1 Cannot Resolve Symbol (Themes, Widget, attr, etc.)

Zakir Shikhli
  • 397
  • 4
  • 14
3

Both resources are defined with the Android Support Library and since these symbols cannot be resolved, it seems like you're missing to define the support dependency.

dependencies {
    compile "com.android.support:appcompat-v7:$androidSupportVersion"
}
tynn
  • 38,113
  • 8
  • 108
  • 143
1

I have solved by,

  • Deleting the .idea folder in project explorer.
  • Invalidate Caches/Restart
  • Refresh Project Gradle
0

In my case help adding maven to build.gradle Project in buildscripts.repositories AND allprojects.repositories. It looks like this:

google()
maven { url 'https://maven.google.com' }
jcenter()
SerjantArbuz
  • 982
  • 1
  • 12
  • 16
0

This is what solved to me:

  1. Close Android Studio
  2. Delete .idea directory in the root project directory

enter image description here

  1. Reimport the project

enter image description here

DragonT
  • 501
  • 1
  • 6
  • 13
0

I updated my gradle version to 4.10.1 and fix and reimport repaired it. There were no libraries folder under .idea before.

ozge s
  • 11
  • 2
0

If you use applicationContext for LayoutInflater, then replace it to normal context. It works for me.

zsxeee
  • 56
  • 5