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
The class has changed to:
If you want to use the old class names, you need to add
android:enableJetifier=true
to yourgradle.properties
and use the appcompat dependencies instead.I think the
Best Approach
should be like, understanding the problem statement first.Scenario:
Solution :
Go to Artifact Mappings page of Android Developer Site and Search for your desired library.
Now go to AndroidX Releases page and check for the current version number
now go to
build.gradle[module.app]
add the library you want. (ie. CardView in my case) and pressSync Now
and necessary files will be added to your project by gradle.See the image below:
Now in xml file you should get CardView or something you have just imported.
implementation com.google.android.material:material:1.0.0
to
implementation com.google.android.material:material:1.0.0-alpha1
it is download all design related class. its work for Me
Add 'androidx.cardview:cardview:1.0.0' in gradle dependencies and modify xml accordingly.
You have 2 different options:
Just add:
and in your xml you can use:
Just add:
and in your xml you can use:
They are different:
MaterialCardView
extendsandroidx.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 (it uses by deafult the styleWidget.MaterialComponents.CardView
)More info here.
Replace this line
from
to
This will work fine in AndroidX.