This is the below cardview code.
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv"
android:background="@drawable/cardborder"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="5dp">
below is cardborder.xml which I am using as background of cardview
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="1dip"
android:color="#f1efec"/>
<corners android:radius="20dip"/>
</shape>
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.
You need to add another layout inside a card view and then set the background for that layout.
For card_view you can only set background color.
I suggest another solution.
My cardviewItem:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="3dp"
card_view:cardCornerRadius="7dp"
card_view:cardElevation="1dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/cardview_body">
<TextView *** />
<CheckBox *** />
<ImageView *** />
</android.support.constraint.ConstraintLayout>
And cardview_body.xml from drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape>
<solid android:color="@color/DarkGrey"/>
<corners android:radius="6dp" />
<stroke android:width="1dp" android:color="@android:color/black" />
</shape>
</item>
</layer-list>
</item>
Important is the fact that the radius in cardview_body.xml is smaller than the radius in cardviewItem. Thanks to this, there is no spare white space at the corners.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With