Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 892
some user Avatar asked Jan 29 '20 22:01

some user


People also ask

How do I customize my CardView?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.

Is CardView deprecated?

This field is deprecated.

What is Material Card View in android?

MaterialCardView is a customizable component based on CardView from the Android Support Library. MaterialCardView provides all of the features of CardView , but adds attributes for customizing the stroke and uses an updated Material style by default.


1 Answers

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

like image 172
Gabriele Mariotti Avatar answered Sep 19 '22 01:09

Gabriele Mariotti