2

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.

James Futures
  • 185
  • 2
  • 12

2 Answers2

3

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

And add dependency:

implementation "androidx.cardview:cardview:1.0.0"
Yeldar Nurpeissov
  • 1,786
  • 14
  • 19
  • Adding the change to use androidx.cardview.widget.CardView along with the dependency solved the problem. What's really confusing about all this, to me, is that I can't find this (along with the dependency you mentioned) anywhere in android documentation. – James Futures Oct 13 '20 at 17:46
  • 2
    `support.v7.*` or `support.v4.*` are no longer maintained. They all have migrated to `androidx`. I recommend to you research about **backward compatibility** and [androidx](https://developer.android.com/jetpack/androidx). – Yeldar Nurpeissov Oct 13 '20 at 17:59
  • Migrating to AndroidX is not always a viable option – pjdupreez Jun 03 '22 at 13:14
1

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

Alex Escobar
  • 96
  • 1
  • 9