Using Images as Background in CardView Pre-Lollipop device issues

suggest change

While using an image or colour as a background in a CardView, you might end up with slight white paddings (if the default card colour is white) on the edges. This occurs due to the default rounded corners in the CardView. Here is how to avoid those margins on pre-Lollipop devices.

Use the attribute card_view:cardPreventCornerOverlap="false" on the CardView. In XML, use the following snippet.

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    card_view:cardPreventCornerOverlap="false"
    android:layout_height="wrap_content"> 
      <ImageView
            android:id="@+id/row_wallet_redeem_img"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@drawable/bg_image" />

</android.support.v7.widget.CardView>
  1. In Java like this cardView.setPreventCornerOverlap(false).

Doing so removes an unwanted padding on the Card’s edges. Here are some visual examples related to this implementation.

1 Card with image background in API 21 (perfectly fine)

2 Card with image background in API 19 without attribute (notice the paddings around image)

3 FIXED Card with image background in API 19 with attribute cardView.setPreventCornerOverlap(false) (Issue now fixed)

Also read the documentation. Original Stack Overflow post here.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents