Search code examples
androidmaterial-designandroid-cardviewandroidxmaterial-components-android

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>

Solution

  • 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