Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add cardview in layout xml in AndroidX

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

like image 838
Jack Wilson Avatar asked Oct 31 '18 18:10

Jack Wilson


People also ask

What is Androidx CardView?

androidx.cardview.widget.CardView. A FrameLayout with a rounded corner background and shadow. CardView uses elevation property on Lollipop for shadows and falls back to a custom emulated shadow implementation on older platforms.

How do I customize my card view?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.


3 Answers

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.

like image 148
TheWanderer Avatar answered Sep 17 '22 21:09

TheWanderer


I think the Best Approach should be like, understanding the problem statement first.

Scenario:

  • Search for the library which Google Inc has changed for "AndroidX" and include in project

Solution :

Go to Artifact Mappings page of Android Developer Site and Search for your desired library.

enter image description here

Now go to AndroidX Releases page and check for the current version number

enter image description here

now go to build.gradle[module.app] add the library you want. (ie. CardView in my case) and press
Sync Now and necessary files will be added to your project by gradle.

See the image below:

enter image description here

Now in xml file you should get CardView or something you have just imported.

NB: pictures included here are (the first two) taken from Android Developer's Page which belongs to Google Inc and could be different depending on the time you see. See this post's posting time.

like image 31
MSI Abu Zafar Newton Avatar answered Sep 16 '22 21:09

MSI Abu Zafar Newton


For AndroidX, Add in gradle dependencies

implementation 'androidx.cardview:cardview:1.0.0'

Then in xml-layout use

<androidx.cardview.widget.CardView

like image 45
ugali soft Avatar answered Sep 16 '22 21:09

ugali soft