How to add cardview in layout xml in AndroidX

Mike Lin picture Mike Lin · Oct 31, 2018 · Viewed 90.2k times · Source

How to add cardview in layout xml in AndroidX

After upgraded to AndroidX

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">

gives a error

The following classes could not be found: - android.support.v7.widget.CardView (Fix Build Path, Edit XML, Create Class)

dependencies {

    implementation 'androidx.appcompat:appcompat:1.0.0'

    implementation 'androidx.cardview:cardview:1.0.0'

but I don't know how to use CardView in xml under AndroidX

Thank you

Answer

TheWanderer picture TheWanderer · Oct 31, 2018

The class has changed to:

androidx.cardview.widget.CardView

If you want to use the old class names, you need to add android:enableJetifier=true to your gradle.properties and use the appcompat dependencies instead.