Search code examples
androidandroid-cardviewandroidx

cardBackgroundColor and cardCornerRadius not working in AndroidX


I'm struggling with CardView corner radius and background color with AndroidX libraries.

I've defined my layout as below:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
        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"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="@dimen/retail_card_width"
        card_view:cardCornerRadius="@dimen/card_radius"
        card_view:cardBackgroundColor="@color/baseYellow"
        android:layout_height="@dimen/retail_card_height">
    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <ImageView android:layout_width="match_parent" android:layout_height="match_parent"
                   tools:src="@drawable/ic_fruit_1"
                   app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
                   app:layout_constraintStart_toStartOf="parent"
                   android:scaleType="fitEnd"
                   app:layout_constraintTop_toTopOf="parent"/>
        <ImageView
                android:id="@+id/ivRetailBrand"
                android:layout_width="@dimen/brand_icon_size"
                android:layout_height="@dimen/brand_icon_size"
                tools:src="@drawable/esselunga"
                android:layout_marginTop="@dimen/retail_brand_margin"
                android:background="@drawable/round_outline"
                app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
                android:layout_marginStart="@dimen/retail_brand_margin"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

Unfortunately, neither cardCornerRadius nor cardBackgroundColor seem working on my layout. I can't understand if my issue depends on AndroidX libraries or not.

Here there's my layout preview:

cardview issue


Solution

  • It turned out that the problem was I was mixing cardview androidx library with support recyclerview library. Once I rebuilt the project with:

    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    

    everything turned out fine.