Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set border to my cardview?

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>
like image 998
Yesha Shah Avatar asked Apr 25 '17 12:04

Yesha Shah


People also ask

How do I customize my CardView?

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

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.

like image 180
Dishonered Avatar answered Sep 19 '22 07:09

Dishonered


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.

like image 31
HubertL Avatar answered Sep 22 '22 07:09

HubertL