some user
some user

Reputation: 1775

strokeColor and strokeWidth not working in androidx.cardview.widget.CardView android

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:

border not showing

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

Answers (2)

Gabriele Mariotti
Gabriele Mariotti

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"
        ../>

enter image description here

Upvotes: 15

omz1990
omz1990

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

Related Questions