'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:
Solution 1:[1]
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.
Solution 2:[2]
Try modifying the CardView as:
<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"
android:layout_width="@dimen/retail_card_width"
app:cardCornerRadius="@dimen/card_radius"
app:cardBackgroundColor="@color/baseYellow"
android:layout_height="@dimen/retail_card_height">
Solution 3:[3]
I might be late for this but let me save someone's time in the future. I had this problem on my Android Studio preview
and came to realise that the problem is that the content inside the CardView is not clipped
to the bounds of the CardView, so the corners you see are corners of the ImageView, which is actually clipped when you run the app on emulator/device.
I hope this helps!
Solution 4:[4]
I had a similar problem. I solved it by wrapping the CardView in FrameLayout.
Addition to an answer:
Or you can try using "app" namespace instead of "card_view":
app:cardBackgroundColor="@color/background_color"
app:cardCornerRadius="@dimen/corner_radius"
Solution 5:[5]
I removed this lines in my Manifest file and after that my cardviews are worked prefectly fine
android:hardwareAccelerated="false"
Solution 6:[6]
I don't think it's related to the CardView setup.
Check Your ImageView they can be the reason for corner's not being rounded.
Comment Out Image View Code(whole ConstraintLayout) and check the UI.As i had the Same Issue in Past.
Solution 7:[7]
try to clean and rebuild the project. This worked for me..
Solution 8:[8]
You can create a new drawable file round_corner_colored.xml and paste the following code in that file :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/light_sky_blue"/>
<corners
android:radius="@dimen/_12sdp"/>
</shape>
Now set this drawable the background of cardview as :
android:background="@drawable/round_corner_colored"
Hope it will help !!
Solution 9:[9]
There are brands that force Dark mode
when app installed for the first time this will affect the coloration of views and applying color in CardView will get ignored. You may apply this to your main theme to prevent such event.
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
Solution 10:[10]
For me, It wasn't showing properly in Preview. But it was showing properly on the device. There is some rendering issue. I keep encountering such problems in android studio.
Faced issue in Android Studio 4.2.1 (Mac)
Solution 11:[11]
I upgraded compileSdkVersion from 28 to 29 in app level gradle and the problem solved.
compileSdkVersion 29
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow