I have a CardView with rounded corners, I want to have an ImageView at the top like shown in the example taken from the material design guidelines below.

components_cards8.png

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
     android:id="@+id/card_view"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     card_view:cardCornerRadius="4dp">

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

Then inside the CardView I have this ImageView

<ImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:scaleType="centerCrop"
    android:src="@drawable/default_cover" />

If I have the card_view:cardCornerRadius set to 0dp then the ImageView fits the card like how I want it to.

image_1

However, the material design guidelines state that cards should have rounded corners, and not square corners.

The problem I have is when I set the card_view:cardCornerRadius to something other than 0dp, e.g. 4dp, then the following happens:

image_2

As can be seen, the ImageView does not fit into the CardView.

My question is, how can I make this ImageView fit to the layout of the CardView when it has rounded corners.


Solution 1:

You need to do 2 things :

1) Call setPreventCornerOverlap(false) on your CardView.

2) Put rounded Imageview inside CardView

About rounding your imageview, I had the same problem so I made a library that you can set different radii on each corners. There is one good library(vinc3m1’s RoundedImageView) that supports rounded corners on ImageView, but it only supports the same radii on every corners. But I wanted it to be rounded only top left and top right corners.

Finally I got the result what I wanted like below.

https://github.com/pungrue26/SelectableRoundedImageView

Rounded ImageView inside CardView

Solution 2:

If your image size (width) is fixed with your ImageView width, you just only have to do is change your ImageView attribute to :

android:scaleType="fitXY"

That is. No additional image corner rounding, no busy work. Beside it efficient for app's performance.

Note : my suggestion may not appropriate for small image with large size ImageView.

Solution 3:

CardView with ImageView

I got this to work by putting a RoundedImageView inside of a CardView. Also you need to set the appropriate CardView attributes.

https://medium.com/@etiennelawlor/layout-tips-for-pre-and-post-lollipop-bcb2e4cdd6b2#.kmb24wtkk

Solution 4:

EDIT 2015/09/29

https://github.com/vinc3m1/RoundedImageView added support of rounding of selected corners

You can also use makeramen RoundedImageView https://github.com/vinc3m1/RoundedImageView, and to remove auto padding in CardView for pre LolliPop use

yourCardView.setPreventCornerOverlap(false);

And then set padding you needded to show shadows of cardview

Solution 5:

Make a bck_rounded.xml in drawable folder. Give it a radius that is same as the card_view.

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="4dp" />
    </shape>

Apply inside your imageView: android:background="@drawable/bck_rounded"