43

enter image description here

I'm trying to add CardView to my activity as the official sample. But it doesn't work.

If I run my project directly, it would lead to java.lang.ClassNotFoundException: android.support.v7.widget.CardView .

WildCat
  • 1,961
  • 6
  • 24
  • 35

9 Answers9

100

You should add it to your build.gradle:

dependencies {
    ...
    implementation 'com.android.support:cardview-v7:21.+'
    implementation 'com.android.support:recyclerview-v7:21.+'
    ...
}
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
Eun
  • 4,146
  • 5
  • 30
  • 51
9

With the release of Android 9.0 (API level 28) there is a new version of the support library called AndroidX

And the support library will be discontinued...

Add the following to buld.gradle:app

dependencies {
    ...
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    ...
}
Then Enok
  • 593
  • 5
  • 16
6

Writing my answer to provide:

1) updated version for compileSdkVersion 27.

2) usage of implementation instead of compile - What's the difference between implementation and compile in Gradle?.

3) a complete version number to heed the Avoid using + in version numbers; can lead to unpredictable and unrepeatable builds.., warning shown by Android Studio.

4) a visual for location of build.gradle (Module:app).

5) And because I was not able to write a comment below @Eun's answer.


You need to paste implementation 'com.android.support:cardview-v7:27.1.1' in build.gradle [Module:app] located at, for example: MyFirstApplication -> app -> build.gradle

Paste in following dependencies clause:

dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   ...
   implementation 'com.android.support:appcompat-v7:27.1.1'


   implementation 'com.android.support:cardview-v7:27.1.1'


   ...
}


To open from Android studio itself: (From left side) 1:Project -> Select Android view -> Gradle Scripts -> build.gradle (Module: app)

That is:
(If the image below is not shown You can find the image here.



Using Android Studio 3.1.4

Edit: Updated version numbers.

Vinay Vissh
  • 457
  • 2
  • 9
  • 12
4

It is all because of dependency and android version problems.

Solution:

For RecyclerView:

<androidx.recyclerview.widget.RecyclerView/>

Instead of: <android.support.v7.widget.CardView..

For CardView:

<androidx.cardview.widget.CardView/>

Instead of: <android.support.v7.widget.RecyclerView..

**Or simply search for and drag and drop, CardView, RecyclerView ..etc from palette panel.

For dependencies that supports AndroidX:

implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'

Go To this Article for full description: https://medium.com/@daryllwong/recyclerview-cardview-is-not-working-properly-dependency-problems-in-2019-e813ab48b3d4

Salah
  • 139
  • 1
  • 4
2
dependencies {
    ...
    compile 'com.android.support:cardview-v7:26.+'
    compile 'com.android.support:recyclerview-v7:26.+'
    ...
}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Vivek Hirpara
  • 787
  • 7
  • 17
2

I was facing this issue because of the corruption of cached cardview aar.

Gradle was picking local cached version of cardview. Also works by changing the library version but make sure you are not overriding configurations.all

Solution:

Close Android Studio, In windows goto C:\Users\Qamar\.gradle\caches and search cardview delete your version folder, clean and rebuild.

Using: Android Studio 3.3 and gradle:3.2.1

Community
  • 1
  • 1
Qamar
  • 4,959
  • 1
  • 30
  • 49
0

In your xml file change android.support.v7.widget.CardView to androidx.cardview.widget.CardView

0

Just click on sync now at right top side

  • In order to turn this into a helpful answer it requires [edit]ing for clearer phrasing, details, explanation. – Yunnosch Dec 23 '20 at 19:30
0

To avoid mistakes during adding CardView, you can use Android Studio tools. In the main menu open "Project structure...", then "Dependencies", "+" and "cardview" in the search line.

At the moment Android Studio adds 'androidx.cardview:cardview:1.0.0' line to your build.gradle file. Then you can use "<androidx.cardview.widget.CardView>" in your layouts.

Maybe after a year or two the version or the package name will change.

Verter
  • 181
  • 2
  • 11