Arantik
Arantik

Reputation: 584

How to set equal space between CardViews in RecyclerView?

I have some CardViews in my RecyclerView, and when I want to set space between them by adding margin, I get this:

enter image description here

As you can see, distance between cards is double. If I for example set margin just for top, left, and right, problem is solved but last card will touch the bottom of screen, and I don't want it. Like this:

enter image description here

As you can see, all spaces are equal and good, but there is no space in the bottom. I have a MainActivity.java, activity_main.xml, recyclerview_row.xml, and MyAdapter.java in my project.

This is my activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:context=".MainActivity"
    android:background="#dddddd">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

And this is my recyclerview_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardElevation="2dp"
    app:cardUseCompatPadding="true"
    app:cardPreventCornerOverlap="false"
    android:layout_margin="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:ignore="UseCompoundDrawables">

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:id="@+id/image_view"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text_view"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"/>

    </LinearLayout>

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

My question is how can I set equal distance between these Cards. Thank you.

Upvotes: 3

Views: 5369

Answers (4)

Mehul Kanzariya
Mehul Kanzariya

Reputation: 1348

Change your activity_main.xml as below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:context=".MainActivity"
android:background="#dddddd">

<android.support.v7.widget.RecyclerView
    android:paddingBottom="10dp"
    android:clipToPadding="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_view">
</android.support.v7.widget.RecyclerView>

The magic is done by this line: android:clipToPadding="false"

Upvotes: 4

Shubham Vala
Shubham Vala

Reputation: 1043

Try This in Main Activity,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:context=".MainActivity"
android:background="#dddddd">

<android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:id="@+id/recycler_view">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

Upvotes: 1

AbhayBohra
AbhayBohra

Reputation: 2117

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        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:context=".MainActivity"
        android:paddingBottom="@dimen/dim_10"
        android:background="#dddddd">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recycler_view">
        </android.support.v7.widget.RecyclerView>

    </RelativeLayout>

Upvotes: 0

faran.javed
faran.javed

Reputation: 418

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:context=".MainActivity"
    android:background="#dddddd">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="10dp"
        android:id="@+id/recycler_view">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

Upvotes: 0

Related Questions