rengineer
rengineer

Reputation: 359

Smaller Space Between Card View in a List

I am trying to make the space between card view smaller in a list; currently, the spaces are too large. I've looked everywhere on SO but there doesn't seem to be a good solution so far. I cannot seem to figure out the correct layout to achieve this:

<?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:foreground="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:layout_width="match_parent"
        android:layout_height="100dp"


        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="5dp"
        card_view:cardCornerRadius="30dp"
        card_view:contentPadding="5dp">


    <RelativeLayout android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="16dp"
                    android:orientation="vertical">

        <TextView
                android:id="@+id/sub_txt"
                android:textSize="18sp"
                android:text="Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="italic"
                android:layout_toLeftOf="@+id/img"
                android:layout_below="@+id/txt"
                android:layout_marginStart="20dp"
                />
        <ImageView android:layout_width="30dp"
                   android:layout_height="wrap_content"
                   android:id="@+id/img"
                   android:contentDescription="@string/app_name"/>

        <TextView
                android:id="@+id/txt"
                android:textSize="22sp"
                android:text="Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_toRightOf="@+id/img"
                android:layout_marginStart="20dp"/>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

enter image description here

Upvotes: 0

Views: 801

Answers (4)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15433

Try removing cardUseCompatPadding and add margin instead like below:

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="100dp"

    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"

    card_view:cardElevation="5dp"
    card_view:cardCornerRadius="30dp"
    card_view:contentPadding="5dp">

Upvotes: 3

Yugansh Tyagi
Yugansh Tyagi

Reputation: 656

Change your CardView height to wrap_content and then you can set your RelativeLayout height to match_parent.

You have given 100dp to your CardView and the RelativeLayout height is smaller than that which leads to more space showing between the items.

Upvotes: 0

Ajeett
Ajeett

Reputation: 812

CardView by default adds extra padding in platforms pre-Lollipop so as to draw shadows. In Lollipop devices, you will have to set cardUseCompatPadding=true, use the line below.

card_view:cardUseCompatPadding="true" 

Upvotes: 0

Hagar Magdy
Hagar Magdy

Reputation: 313

Try to make your card height wrap content

android:layout_height="wrap_content"

Upvotes: 0

Related Questions