ip696
ip696

Reputation: 7084

How can I remove space beetwen cardView in RecyclerView?

How can I remove this spaces beetween cardViews?

image

It is my Layout with cardView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="3dp">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        style="@style/ProductsCardViewStyle">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center_horizontal"
                android:padding="9dp"/>

            <TextView
                android:id="@+id/nameText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="name"
                android:textColor="@android:color/black"
                android:ellipsize="end"
                android:minLines="2"
                android:maxLines="2"
                android:singleLine="false"/>

            <TextView
                android:id="@+id/amountText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="amount"
                android:textColor="@android:color/black"
                android:textStyle="bold" />

            <Button
                android:id="@+id/toBasket"
                android:background="@drawable/round_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Купить" />

        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

It is style. I add this stile to my cardView/ I tried change cardElevation value and I thik use cardElevation with minus value - is wrong. How can I change this spaces?

<style name="ProductsCardViewStyle" parent="Theme.AppCompat.Light">
    <item name="cardCornerRadius">17dp</item>
    <item name="cardElevation">1dp</item>
    <item name="contentPaddingBottom">4dp</item>
    <item name="contentPaddingTop">4dp</item>
    <item name="contentPaddingLeft">4dp</item>
    <item name="contentPaddingRight">4dp</item>
    <item name="cardUseCompatPadding">true</item>
    <item name="cardBackgroundColor">@color/products_item_color</item>
</style>

Upvotes: 0

Views: 840

Answers (2)

yoAlex5
yoAlex5

Reputation: 34215

You can remove android:padding at outer ViewGroup around RecyclerView and cardUseCompatPadding

cardUseCompatPadding : Adds space around the card view to prevent card’s shadow being clipped by the card’s parent view.

You are able to see a result in preview window using

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layoutManager="android.support.v7.widget.GridLayoutManager"
    tools:listitem="@layout/test_item"
    tools:spanCount="2" />

Also please avoid using nested views. More here - https://android-developers.googleblog.com/2017/08/understanding-performance-benefits-of.html

Upvotes: 0

Santanu Sur
Santanu Sur

Reputation: 11477

Remove android:padding="3dp" from your linear layout at the top or reduce it.. to 1dp

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="1dp" //reduce the space like this>

As your grid is horizontal a padding of

  1. 3dp + 3dp total : - 6dp is causing so much space..

So,

1 dp padding will produce a space of total 2dp space between your cards.

Upvotes: 4

Related Questions