Search code examples
javaandroidkotlincardview

border around a cardview


I want to put a blue border around my cardview, but I can't put it on. It works on Constraint Layout tag but not on CardView

card_edge.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="#FFFFFF" />
  <stroke android:width="1dip" android:color="#3683D6" />
  <corners android:radius="5dp" />
</shape>

item_game_list.xml

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true">

    <androidx.cardview.widget.CardView
        android:background="@drawable/card_edge"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="8dp"
        app:cardCornerRadius="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        .
        .
        .

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Use your drawable file i.e. @drawable/card_edge inside the child layout (in my case LinearLayout but You can use any layout as per your requirements.) of cardview and it will create UI as per your requirement.

    <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:clickable="true"
    android:focusable="true"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="8dp"
        app:cardCornerRadius="8dp"
        android:padding="5dp"
        android:elevation="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@drawable/card_edge"
            >
            .
            .
            .
        </LinearLayout>
    
    </androidx.cardview.widget.CardView>
    
    </androidx.constraintlayout.widget.ConstraintLayout>