I am implementing card view but I can\'t find any border option to set a border on it.
here is my card.xml:
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:
here is the solution for your problem:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#00ff00"/>
<corners android:radius="20dip"/>
</shape>
use it as background drawable of your layout
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
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>
Though the question is very old, it may help others who face this problem. Just add the new MDC library in build.gradle
implementation 'com.google.android.material:material:1.2.0-alpha05'
then in your layout add MaterialCardView
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/CardViewStyle"
app:strokeColor="@color/your_color"
app:strokeWidth="@dimen/_1sdp">
</com.google.android.material.card.MaterialCardView>
in style.xml
<style name="CardViewStyle" parent="Widget.MaterialComponents.CardView">
<item name="cardBackgroundColor">@color/white</item>
<item name="cardForegroundColor">@color/transparent</item>
<item name="cardElevation">0dp</item>
<item name="rippleColor">@color/colorRipple</item>
</style>
I did this a little simpler by making the cardBackgroundColor
green and contentPadding
1dp
and inside the card having a ConstraintLayout
(or other layout) with a white background, like this:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:cardBackgroundColor="@color/colorAccent"
app:contentPadding="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="8dp">
...
This way no extra xml or other superfluous code is needed.
Result: