Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 726
orelzion Avatar asked Feb 11 '15 09:02

orelzion


4 Answers

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>
like image 128
Noman Rafique Avatar answered Nov 15 '22 14:11

Noman Rafique


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.

like image 30
PPartisan Avatar answered Nov 15 '22 13:11

PPartisan


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

like image 22
EminenT Avatar answered Nov 15 '22 15:11

EminenT


(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

like image 23
M.Baraka Avatar answered Nov 15 '22 15:11

M.Baraka