Amrutha Saj
Amrutha Saj

Reputation: 1428

Unable to add margin between items in horizontal RecycleView

I use horizontal RecyclerView, the margin between my list item is constant, I want to increase the list item gap, I tried increasing the layout_margin but there is no change.

Below is my list item layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:background="@color/colorWhite"
app:cardElevation="2dp"
app:cardCornerRadius="5dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:selectableItemBackground"
    android:orientation="vertical"
    android:padding="0dp">

    <ImageView
        android:id="@+id/itemImage"
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher_background" />


    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/itemImage"
        android:gravity="center"
        android:padding="5dp"
        android:text="Sample title"
        android:textColor="@color/colorTextBlack"
        android:textSize="16dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="14dp"
        android:gravity="center"
        android:textSize="12dp"
        android:textColor="#9f9f9f"
        android:text="2 Aug, London" />

</LinearLayout>

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

This is the view I am getting, I want to increase the gap between the items enter image description here

can someone help me with the issue?

Upvotes: 2

Views: 1236

Answers (3)

kewei xia
kewei xia

Reputation: 111

You can simply add some padding to your cardview in list item layout.

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="30dp"
    android:background="@color/colorWhite"
    android:paddingStart="10dp"
    android:paddingEnd="10dp"
    app:cardElevation="2dp"
    app:cardCornerRadius="5dp"
    app:cardPreventCornerOverlap="false"
    app:cardUseCompatPadding="true">

Upvotes: 0

Kuls
Kuls

Reputation: 2067

You can change your XML like this and can try,

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

      <android.support.v7.widget.CardView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_margin="10dp"
           android:background="@color/colorAccent"
           app:cardCornerRadius="5dp"
           app:cardElevation="2dp"
           app:cardPreventCornerOverlap="false"
           app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:selectableItemBackground"
        android:orientation="vertical"
        android:padding="0dp">

        <ImageView
            android:id="@+id/itemImage"
            android:layout_width="170dp"
            android:layout_height="170dp"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10dp"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />


        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/itemImage"
            android:gravity="center"
            android:padding="5dp"
            android:text="Sample title"
            android:textColor="@color/cardview_dark_background"
            android:textSize="16dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="14dp"
            android:gravity="center"
            android:text="2 Aug, London"
            android:textColor="#9f9f9f"
            android:textSize="12dp" />

    </LinearLayout>

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

Upvotes: 2

Mayank Bhatnagar
Mayank Bhatnagar

Reputation: 1346

Try like this

recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),
            DividerItemDecoration.VERTICAL));

For customizing it a little bit you can add a custom drawable:

DividerItemDecoration itemDecorator = new DividerItemDecoration(new 
DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
itemDecorator.setDrawable(ContextCompat.getDrawable(getActivity(), 
R.drawable.divider));

divider.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
<solid android:color="@color/colorPrimary"/>
<size android:height="0.5dp"/> //change height acc to your need.

Upvotes: 0

Related Questions