3

I am trying to center a RecyclerView when its layout_width is wrap_content without success

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_schemes"
        android:scrollbars="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

When the RecyclerView is given any definite layout_width of say 200dp then it does center otherwise it just aligns left.

How to make the RecyclerView center_horizontal when its layout_width is wrap_content ?

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Lakshman Chilukuri
  • 1,067
  • 2
  • 11
  • 20

3 Answers3

11

According to your question, update on (march 2016)

I suggest you to update to Android Support Library 23.2.1 to support WRAP_CONTENT in RecycleView.

In RecyclerView layout_width to wrap_content was not supported before 23.2.1.

solved some issue like Fixed bugs related to various measure-spec methods

Check http://developer.android.com/tools/support-library/features.html#v7-recyclerview

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • 1
    Please don't post identical answers to multiple questions. Post one good answer, then vote/flag to close the other questions as duplicates. If the question is not a duplicate, _tailor your answers to the question_. – josliber Feb 28 '16 at 20:34
  • okay. i have change content of my answer and will follow your advice. – Amit Vaghela Mar 07 '16 at 10:42
10

It is related on how the LayoutManager calculates the sizes. Here you have the related bug with some workarounds people have used.

NOTE: With the release 23.2 of the support library now the wrap content is supported, so looks like they fixed it. You can checkout the changelog here

droidpl
  • 5,872
  • 4
  • 35
  • 47
-2

Did you try using a RelativeLayout instead of Linear Layout and then make the Recyclerview layout_centerHorizontal="true"

Deepak Baliga
  • 245
  • 2
  • 7