Raunak Verma
Raunak Verma

Reputation: 211

how to set equal margin between recyclerview items

This image

See this Image

I achieved the current margin from xml layout but its not same through all four sides.

How to get equal margin

My current xml layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp">

<ImageView
    android:id="@+id/iv_photo"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_marginEnd="6dp"
    android:layout_marginTop="6dp"
    android:adjustViewBounds="true"
    android:background="@color/colorPrimary"
    android:foreground="?android:attr/selectableItemBackground"
    android:scaleType="fitXY" />

</RelativeLayout>

Upvotes: 6

Views: 13726

Answers (3)

Hari Babu Koduri
Hari Babu Koduri

Reputation: 41

In RecyclerView give only Left and bottom margins

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

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

and in item_recyclerview_layout for the recyclerView item give top and right margins

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:adjustViewBounds="true"
android:padding="10dp"
android:background="@drawable/border">

<ImageView
    android:id="@+id/logo"
    android:layout_width="70dp"
    android:layout_height="70dp"
    app:srcCompat="@drawable/rsz_honda_bike" />

<TextView
    android:id="@+id/name"
    android:textStyle="bold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="15sp"
    android:layout_gravity="center"/>
</LinearLayout>

Upvotes: 3

Zeeshan Shabbir
Zeeshan Shabbir

Reputation: 7114

You can use ItemDecoration for this purpose

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
  private int space;

  public SpacesItemDecoration(int space) {
    this.space = space;
  }

  @Override
  public void getItemOffsets(Rect outRect, View view, 
      RecyclerView parent, RecyclerView.State state) {
    outRect.left = space;
    outRect.right = space;
    outRect.bottom = space;

    // Add top margin only for the first item to avoid double space between items
    if (parent.getChildLayoutPosition(view) == 0) {
        outRect.top = space;
    } else {
        outRect.top = 0;
    }
  }
}

This is how you will use it in java code

mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
mRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));

This solution was provided by @ianhanniballake. Hope this will help you.

Upvotes: 17

Bipin Gawand
Bipin Gawand

Reputation: 561

use DividerItemDecoration class.
for reference follow this link How to add dividers and spaces between items in RecyclerView?

Upvotes: 0

Related Questions