I want to corner only the top of a cardview.
I used below property and it is rounding all the corner.
I want to show an overlap of all cards
card_view:cardCornerRadius="4dp"
here is my layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
card_view:cardPreventCornerOverlap="false"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/re1">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="@color/colorAccent"
android:text="contact det"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:textSize="14dp"/>
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="@id/title"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"/>
<TextView
android:id="@+id/txtSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="@id/txtName"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="@+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="10dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="150dp"
android:layout_alignBaseline="@id/txtName"/>
<TextView
android:id="@+id/txtAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textSize="10dp"
android:layout_alignLeft="@id/txtEmail"
android:layout_alignBaseline="@id/txtSurname"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
I have been trying the same but none of the solutions provided worked for me.
The only thing that worked was:
1) Make a custom background resource (like a rectangle shape) with rounded corners.
2) set this custom background using the command -
Worked perfectly for me! Hope this helps you.
The XML layout I made with top left and bottom right radius.
In your case, you need to change only topLeftRadius as well as topRightRadius.
Tricky thing because you can't make CardView do this. Internally it uses a
RoundRectDrawable
(package private) which usesroundRect
like this:Therefore you need a different solution, for example I found this gist by Ahmed-Abdelmeged where they use canvas clipping per each corner using a path to describe the outline.
So while I'm not the one who made this code, I'll post it here for future travellers.
and
This will let you clip the edge of images and views before they get rendered, therefore it does exactly what you want.
We can set the marginBottom of the card view in negative value.Margin should be same value as card radius. For Example,
It works for me.But I am in doubt whether it is the proper way of doing.Any suggestions are welcome.
As per the question, I am assuming that you want to apply corner radius property to only top of the card. You can obtain this effect by using two
CardView
. Place oneCardView
inside anotherCardView
and remove outerCardView
corner radius property. Also apply a transparent background to your outerCardView
Your inner CardView will have a cornerRadius value of 4dp. Then apply a marginTop to your innerCardView
, so that its bottom part becomes hidden by the outerCardView
. This way, the bottom corner radius of your innerCardView
will be hidden.You will have to put your xml content in your Inner
CardView
. OuterCardView
serves only the purpose of hiding the bottom rounded corners of the innerCardView
. Your xml layout will look like this:I have taken reference from this SO question: Question. I hope it solves your problem.