1

I have tried CardView library on the older android studio version 3.3.1 and the code was working. The emulator was displaying the correct images. But, I switched to the new version 3.5 nothing is displaying in the CardView, not even a TextView.

xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:padding="10dp"
    android:background="#fcfcfc"
    android:gravity="center"
    android:id="@+id/LinearLayout1">

    <!-- A CardView that contains a TextView -->
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp"
        card_view:cardCornerRadius="4dp">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v7.widget.CardView>

</LinearLayout>

Gradle:

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.android.support:cardview-v7:28.0.0'

}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Hello World
  • 47
  • 1
  • 7
  • 1
    google replaced all materia desine componentd to single library implementation 'com.google.android.material:material:1.0.0', just import this lib and remove cardview lib – Harish Reddy Oct 21 '19 at 08:20
  • @HarishReddy It is not totally correct. There is also an andriodx implementation of CardView. – Gabriele Mariotti Oct 21 '19 at 12:05

3 Answers3

4

You should use

<androidx.cardview.widget.CardView //XML Section

Add AndroidX dependency

implementation 'androidx.cardview:cardview:1.0.0'
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    It is not totally correct. There are 2 different implementations of `CardView`. The androidx library (`androidx.cardview.widget.CardView`) and the material components library (`com.google.android.material.card.MaterialCardView`). If you want to use the first one, you don't need the material compoments library. – Gabriele Mariotti Oct 21 '19 at 12:04
  • @GabrieleMariotti indeed sir. – IntelliJ Amiya Oct 21 '19 at 12:12
2

You can't use support and andriodx libraries together.

Change

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

to

implementation 'androidx.cardview:cardview:1.0.0'

And use androidx.cardview.widget.CardView in your layout/code.

There is another implementation of the CardView.
It is included in the Material Components Library

Just add:

implementation 'com.google.android.material:material:1.1.0'

and in your xml you can use:

 <com.google.android.material.card.MaterialCardView
  ...>

They are different:

MaterialCardView extends androidx.cardview.widget.CardView and provides all of the features of CardView, but adds attributes for customizing the stroke and uses an updated Material style by default.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

I had the same problem in Android Studio 3.5.1. It worked for me:

dependencies {

`def cardview_version = "1.0.0" implementation "androidx.cardview:cardview:$cardview_version" }

and xml: