how to set colored border on cardview

With the material components library just use the MaterialCardView with the app:strokeColor and app:strokeWidth attributes.

Note that without an app:strokeColor, the card will not render a stroked border, regardless of the app:strokeWidth value (the default values are app:strokeColor=@null and app:strokeWidth=0dp).

Something like:

<com.google.android.material.card.MaterialCardView
    ...
    app:strokeWidth="1dp"
    app:strokeColor="@color/stroke_color"
    app:cardElevation="xxdp">

    ...

</com.google.android.material.card.MaterialCardView>

enter image description here


With Jetpack Compose 1.0.x you can use the border attribute in the Card composable:

Card( border= BorderStroke(1.dp, Red)){
    //.....
}

enter image description here


Started from v28 design support library we can use Material Card View, which provides us with a material styled cardview implementation out of the box.

<android.support.design.card.MaterialCardView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_margin="10dp">
    ... child views ...
</android.support.design.card.MaterialCardView>

You can further style the cardview by using two of the attributes that come with it:

  • app:strokeColor - The colour to be used for the given stroke, this must be set in order to display a stroke
  • app:strokeWidth - The width to be applied to the stroke of the view

Create drawable selector.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#808080"/>
    <stroke android:width="3dp" android:color="#B1BCBE" />
    <corners android:radius="20dp"/>
    <padding android:left="0dp" android:top="0dp"
             android:right="0dp" android:bottom="0dp" />
</shape>      

then give set this as a background, change color according your choice