Android CardView remove padding

Solution 1:

UDPATE

Well, seems there is a much easier way to do it, without guessing the padding and all:

card_view:cardPreventCornerOverlap="false"

or using java:

cardView.setPreventCornerOverlap(false)

You can read all about it here.

ORIGINAL ANSWER

It is an intentional padding to avoid content from bleeding off the rounded corner of the card in pre lollipop versions (since clipping is not available). If you want to get rid of it all together you may use a negative padding in pre-lollipop versions for the contentPadding attribute of the CardView such as:

card_view:contentPadding="-8dp"

or using java:

int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
cardView.setContentPadding(-padding,-padding,-padding,-padding);

I'm not particularly sure which value will work seamlessly, you'll need to experiment and see for yourself.

Solution 2:

Please remove this from your code -

card_view:cardUseCompatPadding="true"

Solution 3:

Got the same problem and this worked for me.

app:cardPreventCornerOverlap="false"

For device with API < 21, I had to make a custom ImageView class, override the onDraw to draw the round corners.

@Override
protected void onDraw(Canvas canvas) {
    float radius = getContext().getResources().getDimension(R.dimen.card_corner_radius);
    Path path = new Path();
    RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
    path.addRoundRect(rect, radius, radius, Path.Direction.CW);
    canvas.clipPath(path);
    super.onDraw(canvas);
}

Solution 4:

use this:

cardView.setPadding(0,0,0,0);
cardView.setUseCompatPadding(true);
cardView.setContentPadding(-6,-6,-6,-6);
cardView.setPreventCornerOverlap(false);