How to add cardview in layout xml in AndroidX

AndroidAndroidx

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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] 6 which belongs to Google Inc and could be different depending on the time you see. See this post's posting time.

Solution 3 - Android

For AndroidX, Add in gradle dependencies

implementation 'androidx.cardview:cardview:1.0.0'

Then in xml-layout use

<androidx.cardview.widget.CardView

Solution 4 - Android

You have 2 different options:

Just add:

implementation 'com.google.android.material:material:1.2.0'

and in your xml you can use:

 <com.google.android.material.card.MaterialCardView
  ...>
  • Androidx CardView

Just add:

implementation 'androidx.cardview:cardview:1.0.0'

and in your xml you can use:

<androidx.cardview.widget.CardView>

They are different:

MaterialCardView extends androidx.cardview.widget.CardView and provides all of the features of CardView and

  • add support for checking and dragging

  • uses a MaterialComponents theme by default (it uses by deafult the style Widget.MaterialComponents.CardView)

  • support the ShapeAppearanceModel

  • adds attributes for customizing the stroke

Solution 5 - Android

Add 'androidx.cardview:cardview:1.0.0' in gradle dependencies and modify xml accordingly.

Solution 6 - Android

Replace this line

from

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"

to

<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"

This will work fine in AndroidX.

Solution 7 - Android

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

Solution 8 - Android

I had the same problem in Android Studio 3.5.1. It worked for me:

def cardview_version = "1.0.0"
implementation "androidx.cardview:cardview:$cardview_version"


xmln: <androidx.cardview.widget.CardView>....

Solution 9 - Android

Changes in : 1. build.gradle(:app)

dependencies {
...
implementation 'androidx.cardview:cardview:1.0.0' 
....
}

2. in xml section:

<androidx.cardview.widget.CardView>
...
</androidx.cardview.widget.CardView>

Solution 10 - Android

 implementation 'androidx.cardview:cardview:1.0.0' // must add this ``


               <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginLeft="25dp"
                android:layout_marginTop="20dp"
                android:id="@+id/game_id"
                android:layout_marginRight="25dp"
                app:cardBackgroundColor=""
                app:cardCornerRadius="15dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:orientation="horizontal"
                    android:layout_height="match_parent">

                    <ImageView
                        android:layout_width="130dp"
                        android:layout_height="170dp"
                        android:layout_gravity="center"
                        android:layout_marginLeft="5dp"
                        android:background="@drawable/gamess">

                    </ImageView>


                    <TextView
                        android:layout_width="130dp"
                        android:layout_marginLeft="20dp"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/game"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="10dp"
                        android:gravity="center"
                        android:text="Gamensjnjdhddgsgddgjsdjsdgjsgdjsgddgjsdjsdjg"
                        android:textColor="@color/white_color"
                        android:textSize="16dp"
                        android:textStyle="bold">

                    </TextView>
                </LinearLayout>

            </androidx.cardview.widget.CardView>

> [![Blockquote][1]][1]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJack WilsonView Question on Stackoverflow
Solution 1 - AndroidTheWandererView Answer on Stackoverflow
Solution 2 - AndroidMSI Abu Zafar NewtonView Answer on Stackoverflow
Solution 3 - Androidugali softView Answer on Stackoverflow
Solution 4 - AndroidGabriele MariottiView Answer on Stackoverflow
Solution 5 - AndroidRakesh KumarView Answer on Stackoverflow
Solution 6 - AndroidJerry ChongView Answer on Stackoverflow
Solution 7 - AndroidKashif khanView Answer on Stackoverflow
Solution 8 - AndroidVictor MoralesView Answer on Stackoverflow
Solution 9 - Androidabir-cseView Answer on Stackoverflow
Solution 10 - AndroidAqifView Answer on Stackoverflow