orelzion
orelzion

Reputation: 2532

RecyclerView gravity

I have a recylcler view, but it seems that does not implement the setGravity method.

My list is horizontally aligned, but I need it to be centered. How do I do that?

fragment_my_zone_item_controlled.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_grey">

    <FrameLayout
        android:id="@+id/fragment_my_zone_item_controlled_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/fragment_my_zone_item_controlled_tabs"
        android:layout_marginBottom="12dp"
        android:layout_marginTop="12dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_my_zone_item_controlled_tabs"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:background="@color/action_bar_color"
        android:scrollbars="none" />

</RelativeLayout>

This is how it looks

This is how it looks, I need it to be centered

Thank you guys

Upvotes: 11

Views: 12877

Answers (4)

nomanr
nomanr

Reputation: 3775

theLazyFinder solution works but just in case if you want to have a different background color for RV then add it to FrameLayout and set its gravity to center.

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorWhite">

        <android.support.v7.widget.RecyclerView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />

</FrameLayout>

Upvotes: 1

M.Baraka
M.Baraka

Reputation: 755

(In case someone is looking for answer like me), I tried both solution and didn't work for me, here is what I did. I calculated the screen width & my view width, then in my RecyclerView.ViewHolder I added margin to the left for me view (it was a ratio in my case)

DisplayMetrics metrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;
long ratio = screenWidth * 90 / 100;
int margin = (int) ((screenWidth - ratio) / 2);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams((int) ratio, ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.gravity = Gravity.CENTER_VERTICAL;
lp1.setMargins(margin, 0, 0, 0);
itemLayoutView.setLayoutParams(lp1);

It is messy solution but I couldn't find another

Upvotes: 1

PPartisan
PPartisan

Reputation: 8231

If the other answers don't help you, I faced a problem in RecyclerView whereby content was not wrapped effectively, which led to layouts being forced to one corner or the other.

The answer for me was to create a custom class that extended LinearLayoutManager. You may be able to try this and then set the gravity programmatically.

Here is a link to my question, and the post where I got the idea for my answer.

Upvotes: 1

Amarjit
Amarjit

Reputation: 4357

I know this is very late answer , May be some one other got help from this so please try to add like this

 <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_my_zone_item_controlled_tabs"
        android:layout_width="wrap_content"
        android:layout_height="44dp"
        android:layout_centerHorizontal="true"
        android:background="#dddddd"
        android:scrollbars="none" />

add android:layout_centerHorizontal="true" method

Upvotes: 6

Related Questions