CardView error: Cannot resolve class android.support.v7.widget.CardView

9k Views Asked by At

I'm trying to use cardview with my recyclerview. The recycler view works on its own. I've got it set up with databinding. But I wanted the recyclerview to look better which is why I'm wanting to use cardview with it.

This is my layout file for individual views:

<layout 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">

<data>
    <variable
        name="individualEntry"
        type="com.zebotek.balancedmindjournal.persistance.JournalEntryDataClass" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

        <TextView
            android:id="@+id/dateTextBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            app:dateStringFormatted="@{individualEntry}"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/journalEntryTextBox"
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"
            android:textSize="14sp"
            app:journalEntryText="@{individualEntry}"
            app:layout_constraintTop_toBottomOf="@+id/dateTextBox" />

    </android.support.v7.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

and this is my dependencies:

cardView_version = '28.0.0'    

implementation "com.android.support:cardview-v7:$cardView_version"
implementation "com.android.support:appcompat-v7:$cardView_version"
implementation "com.android.support:recyclerview-v7:$cardView_version"

I can't identify anything that I could do differently. I have minSdkVersion set at 21.

The code works just fine without the cardview. If I add the cardview, now it won't compile and I get that error. I've even tried placing the cardview above the constraintlayout. I've replaced the constraintlayout with the cardview and even replaced the top level layout with the cardview.

2

There are 2 best solutions below

3
On BEST ANSWER

Try to use androidx.cardview.widget.CardView, instead of android.support.v7.widget.CardView.

And add dependency:

implementation "androidx.cardview:cardview:1.0.0"
0
On

If you use androidx(It is seen in your first layout), I still recommend using CardView from AndroidX

just import in yout build.gradle

dependencies {
    implementation "androidx.cardview:cardview:1.0.0"
    implementation "androidx.appcompat:appcompat:1.2.0"
    implementation "androidx.recyclerview:recyclerview:1.1.0"
}

Another recommendation is to remove the ConstraintLayout and leave only CardView