material design – Android CardView with rounded corners displays grey corners-ThrowExceptions

Exception or error:

I’m inflating a custom layout with CardView inside the layout.
The rounded corners are displayed as expected but I also get grey background behind the corners.

The code is simple, uses a CardView with corner radius and a background color.
I’ve tried setting transparent background but doesn’t work. However, if i set another opaque color, it is displayed in corners.

Code is attached.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@android:color/white"
        app:cardCornerRadius="6dp"
        app:cardElevation="5dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent">

            <TextView
                android:id="@+id/tvProgress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/ivIcon"
                android:layout_toStartOf="@+id/ivIcon"
                android:background="@android:color/transparent"
                android:padding="@dimen/elementPaddingSmall"
                android:text="Initial Discussion"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/black" />

            <ImageView
                android:id="@+id/ivIcon"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:background="@color/lightBrown"
                android:scaleType="centerInside"
                android:src="@drawable/ic_checkmark_circle" />
        </RelativeLayout>


    </android.support.v7.widget.CardView>
</RelativeLayout>

Result:

enter image description here

How to solve:

It is because of shadow, you need to give space to cardview to show full shadow. Add android:layout_margin="5dp" to CardView and you will see that the “grey” color is cut shadow.

Solution is adding app:cardUseCompatPadding="true" to CardView and it will give needed spacing.

###

Try this…

Just set 0 value to app:cardElevation

.....
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@android:color/white"
    app:cardCornerRadius="6dp"
    app:cardElevation="0dp">
.....

OR you can call cardView.setCardElevation(0) to disable shadow programmatically.

###

Remove the parent RelativeLayout that is wrapping the CardView and you are good to go. Just like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@android:color/white"
    app:cardCornerRadius="6dp"
    app:cardElevation="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent">

        <TextView
            android:id="@+id/tvProgress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/ivIcon"
            android:layout_toStartOf="@+id/ivIcon"
            android:background="@android:color/transparent"
            android:padding="@dimen/elementPaddingSmall"
            android:text="Initial Discussion"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/black"/>

        <ImageView
            android:id="@+id/ivIcon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="@color/lightBrown"
            android:scaleType="centerInside"
            android:src="@drawable/ic_checkmark_circle"/>
    </RelativeLayout>


</android.support.v7.widget.CardView>

###

If someone faces the problem when the edges’ color is darker than you set, it could happen if you set the color in #AARRGGBB format. To fix this issue, just set the color in the normal #RRGGBB format.

###

Change app:cardUseCompatPadding="true" to card_view:cardUseCompatPadding="true"

Leave a Reply

Your email address will not be published. Required fields are marked *