1

I have created the following layout for my RecyclerView and I am displaying it in the grid style.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:paddingBottom="24dp"
        android:background="@drawable/shadow_player_stats"
        xmlns:app="http://schemas.android.com/apk/res-auto">



    <TextView
            android:id="@+id/tvPlayerStats"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="12dp"
            android:letterSpacing="0.1"
            android:maxLength="4"
            android:maxLines="1"
            android:text="-"
            android:includeFontPadding="false"
            android:textColor="@color/white"
            android:textSize="34dp"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="@+id/ivGames"
            app:layout_constraintTop_toTopOf="@+id/ivGames" />

        <TextView
            android:id="@+id/tvLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginEnd="12dp"
            android:maxLines="2"
            android:lines="2"
            android:text="DATA-TEST"
            android:textAllCaps="true"
            android:textColor="@color/white"
            android:textSize="14dp"
            android:textStyle="normal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/ivGames"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="@+id/ivGames"
            app:layout_constraintTop_toBottomOf="@+id/tvPlayerStats"
            app:layout_constraintVertical_bias="0.0" />


    </androidx.constraintlayout.widget.ConstraintLayout>    

This is generating following type of layout.

enter image description here

But I want to add spacing on the start and at the end of each row, and not in between of items, like following

enter image description here

aminography
  • 21,986
  • 13
  • 70
  • 74
dev90
  • 7,187
  • 15
  • 80
  • 153
  • You can add paddingStart and paddingEnd to the RecyclerView (not to the layout you are inflating and shared in your question.. but to the RecyclerView itself) – guipivoto Nov 06 '19 at 11:10
  • Recyclerview is consist of 3 layouts, 1 of them is linear line, if i add padding or margin to recyclerview, then those lines are not connected to the end. – dev90 Nov 06 '19 at 11:11
  • 1
    Is this helpful? https://stackoverflow.com/questions/29146781/decorating-recyclerview-with-gridlayoutmanager-to-display-divider-between-item – Ivan Wooll Nov 06 '19 at 11:16
  • @IvanWooll : It was really helpful, Thanks for sharing it :) – dev90 Nov 08 '19 at 10:16

2 Answers2

0

Add start/left and end/right padding in recycler view

Try this

  <androidx.recyclerview.widget.RecyclerView
                            android:id="@+id/draft_recylcerview"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" 
                            android:paddingLeft="@dimen/margin_10"
                            android:paddingRight="@dimen/margin_10"/>
Android Geek
  • 8,956
  • 2
  • 21
  • 35
0

Try below solution.

This is my custom layout, containing TextView my_custom_row.xml

<?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="vertical">

    <TextView
        android:id="@+id/myCustomTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="16sp"
        android:textStyle="bold" />

</LinearLayout>

In above code you can see that I have set paddingTop and paddingBottom in TextView, So it will generate layout something like this

enter image description here

Now when I just want to set more margin/padding at the start and end of recyclerview only, I have to set below properties in recyclerview xml code.

android:clipToPadding="false"
android:paddingTop="15dp"
android:paddingBottom="15dp"

which will generate layout something like this

enter image description here

NOTE: I have reduced padding from my textView

Bhavnik
  • 2,020
  • 14
  • 21