Rounded corners cardView don't work in RecyclerView - Android?
It turns out that calling View.setBackgroundColor(int)
on a CardView
removes the rounded corners.
To change the background colour of a card and retain the corners, one needs to call CardView.setCardBackgroundColor(int)
instead.
That may be the case for certain visitors to this post.
When subclassing CardView
, I suggest adding the following method to protect your corners from accidental removal:
/**
* Override prevents {@link View#setBackgroundColor(int)} being called,
* which removes the rounded corners.
*/
@Override
public void setBackgroundColor(@ColorInt int backgroundColor) {
setCardBackgroundColor(backgroundColor);
}
In particular, I was working on a custom view implementation for React Native, and React was automatically applying a background colour to the view. This override solved that problem; it means other developers do not need to know the details of the underlying view.
use this in topmost layout declaration in xml layout file:
xmlns:card_view="http://schemas.android.com/apk/res-auto"
This solved the problem for me.
Try adding this two attributes in your card view
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
Here is the documentation for the first attribute (which contains reference to the second) https://developer.android.com/reference/android/support/v7/widget/CardView.html#setPreventCornerOverlap(boolean)
This should do the trick