1

I have a simple layout with a recycler. I feed it with Adapter and set GridLayoutManager for the recycler.

<android.support.v7.widget.RecyclerView android:id="@+id/scripts_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="50dp" android:verticalSpacing="20dp" android:clipToPadding="false" android:orientation="vertical" app:layoutManager="android.support.v7.widget.GridLayoutManager" tools:listitem="@layout/tmpl_script" android:paddingBottom="20dp" android:layout_marginEnd="20dp" android:layout_marginRight="20dp" android:paddingTop="30dp" app:layout_gravity="center" /> 

Activity

RecyclerView recyclerView = (RecyclerView)findViewById(R.id.scripts_recycler_view); recyclerView.setAdapter(adapter); int columns = getResources().getInteger(R.integer.scripts_columns); recyclerView.setLayoutManager(new GridLayoutManager(this, columns)); recyclerView.setHasFixedSize(true); 

Item

<LinearLayout android:layout_width="300dp" android:layout_height="300dp" android:background="@drawable/game_border" android:gravity="center" android:orientation="vertical"> 

I have two problems:

  1. there is no space between rows
  2. items are not centered within the row

I tried all properties in Android Studio but they do not have any effect. I googled, opened many questions but I still have no luck. What magic property shall I use?

As you can see items are positioned on left side of the screen. I want to move it to center. And there is no space between first and second row.

enter image description here enter image description here

3
  • Anybody with idea, how to center items? I will add another picture with single column. Commented Dec 18, 2016 at 19:17
  • Maybe pass a custom layout params with gravity=center? But I have not found a setter. Commented Dec 18, 2016 at 19:54
  • I swapped to a GridView and tiles are centered finally. Commented Dec 21, 2016 at 5:06

2 Answers 2

2

The easiest solution for (1) is to add top and/or bottom margin, like @suraj said.

Regarding (2) - the critical issue is that your items are square. To get it working, set android:layout_width="match_parent" and change the design of your items to use the actual smaller dimension (width or height) for both dimensions (maybe all you need to do in your case is to make the background square). Then display the content centered within the item's LinearLayout (you are already doing this). You have to do it this way because GridLayoutManager does not support layout_gravity at this time.

Sign up to request clarification or add additional context in comments.

1 Comment

I figured. But since I ran into the same problem as you, with the same detail of a square view, I decided to figure it out and help out the rest of the world.
0

For 1.there is no space between rows

Try android:layout_marginBottom="10dp" in the linear layout of item.This should provide the spacing below each item.

For 2.items are not centered within the row

Looks like you have given right margin to your recyclerview and no left margin.

android:layout_marginLeft="20dp" android:layout_marginStart="20dp" 

should give equal spacing on both sides. or set

app:layout_gravity="center" 

in linear layout of the item.

7 Comments

ad 1) thanks, this works. Why does not it work at parent container?
you are refering to 1 or 2?
ad 2) this could make it better but I want to center the items regardless of current dimensions.
the items were centered with respect to the parent(recyclerview). The parent itself was to the side.
sry.. layout_gravity is centering the grid with respect to the recyclerview
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.