Reputation: 1775
Trying to make some border around cardView
(androidx.cardview.widget.CardView
)
Here's the code:
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="273dp"
android:layout_height="118dp"
android:layout_marginStart="9dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="9dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="vertical"
app:cardCornerRadius="8dp"
android:background="@drawable/fix_border"
app:strokeColor="#dedede"
app:strokeWidth="3dp">
</androidx.cardview.widget.CardView>
Result:
Also tried to use a custom shape as a background (fix_border.xml
), doesn't work either.
fix_border.xml
:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="2dp"
android:color="#dedede" />
<!-- <solid android:color="#ffffff" />-->
<padding
android:bottom="7dp"
android:left="7dp"
android:right="7dp"
android:top="7dp" />
</shape>
Upvotes: 7
Views: 9753
Reputation: 364868
Just use the Material Components Library and the MaterialCardView
which extends the androidx.cardview.widget.CardView
.
Something like:
<com.google.android.material.card.MaterialCardView
app:strokeColor="@color/primaryDarkColor"
app:strokeWidth="3dp"
../>
Upvotes: 15
Reputation: 742
It is not possible to give a drawable background to a CardView
(reference: v7 cardview library issues), you can give it a app:cardBackgroundColor
though, but if you need the stroke, you can add a layout inside it and add the drawable background with the stoke to that and it works. So, something like:
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="273dp"
android:layout_height="118dp"
android:layout_marginStart="9dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="9dp"
android:padding="10dp"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
app:cardCornerRadius="8dp">
<!-- Any layout type here -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fix_border">
<!-- Build your layout, with a background stroke! -->
</RelativeLayout>
</androidx.cardview.widget.CardView>
Upvotes: 0