androidandroid-recyclerviewandroid-cardview

How to create some margins around a CardView in a Recylerview


I have the following XML layout file for a cardView that I then put into a Recylerview:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cardView_itemLevel"
    android:layout_width="150dp"
    android:layout_height="150dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="10dp"
    android:layout_marginTop="12dp"
    android:layout_marginStart="12dp"
    android:layout_marginBottom="12dp"
    android:layout_marginEnd="12dp"

    app:cardPreventCornerOverlap="true"


    app:cardElevation="0dp"
    app:cardUseCompatPadding="false"
    app:contentPaddingBottom="2dp">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <ImageView
            android:id="@+id/imageView_levelImage"
            android:layout_width="75dp"
            android:layout_height="75dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.507"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="5dp"
            />

        <TextView
            android:id="@+id/textView_bestResultThisLevel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:textSize="10sp"

            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView_levelImage" />


        <TextView
            android:id="@+id/textView_CO2SavingsTotalThisLevel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:textSize="10sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_bestResultThisLevel" />


        <TextView
            android:id="@+id/textView_gasSavingsTotalThisLevel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:textSize="10sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_CO2SavingsTotalThisLevel" />



    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

I use the margins value to all directions but still the output looks like this: enter image description here

Unfortunately, on the vertical dimension (the fragment is always displayed in landscape mode) there is no margin around the different cardviews. My question is how can I create such a marging.


Solution

  • Add the cardview to another layout.

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="2.5dp">
    
    <androidx.cardview.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardBackgroundColor="@color/tintBackground"
        app:cardCornerRadius="8dp"
        app:cardElevation="0dp">
    
    <ImageView
        android:id="@+id/image_view_center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:src="@drawable/use_default_icon"/>
    
    </androidx.cardview.widget.CardView>
    
     </RelativeLayout>