Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cardview - white border around card

I am using a cardview as the root of a custom view I am writing. I using the v7 support library. My XML looks like this:

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView         xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:card_view="http://schemas.android.com/apk/res-auto"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_marginRight="6dp"         card_view:cardElevation="0dp">      <LinearLayout             android:layout_width="match_parent"             android:layout_height="match_parent"             android:orientation="vertical">          <!-- some other views -->     </LinearLayout> </android.support.v7.widget.CardView> 

My problem is that I am getting a white border around my card view. It looks like it is there to indicate elevation as it is thicker on the right side. I've tried adjusting cardElevation and MaxCardElevation in my XML like so : card_view:cardElevation="0dp"

and in code in my custom view that extends CardView and uses this layout:

setCardElevation(0); setMaxCardElevation(0); 

But the white border persists. I'm not sure how to get rid of it. If anyone had any input into why this is happening or suggestions on how I can remove the white border it would be appreciated. Thanks much.

like image 666
TheMethod Avatar asked Apr 01 '15 13:04

TheMethod


People also ask

How do I add padding to CardView?

If you want to use CardView padding on pre-L devices, and have it look the same on Lollipop+ devices, then you will need to use setUseCompatPadding(true) , or the XML variant cardUseCompatPadding="true" .

When should I use CardView?

CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI design.

How do I customize my card view?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.


2 Answers

I know it's a bit late, but for anyone having a similar problem:

I had the same issue: A white border was shown on pre-lollipop devices.

I solved it setting the cardPreventCornerOverlap to false on your XML.

Like this:

<android.support.v7.widget.CardView     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:card_view="http://schemas.android.com/apk/res-auto"     android:layout_width="wrap_content"     android:layout_height="match_parent"     android:layout_marginRight="6dp"     card_view:cardPreventCornerOverlap="false">      <LinearLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical">          <!-- some other views -->     </LinearLayout> </android.support.v7.widget.CardView> 

Hope this helps!

like image 99
pol_databender Avatar answered Sep 22 '22 21:09

pol_databender


Support CardView doesn't support content clipping, because it's expensive on older devices. It's possible to clip content using Canvas.saveLayer/restoreLayer and PorterDuff modes. This is how Carbon implements rounded corners with correct content clipping. See the image:

enter image description here

like image 29
Zielony Avatar answered Sep 20 '22 21:09

Zielony