Loolooii
Loolooii

Reputation: 9190

Is it possible to set gravity programmatically for RecyclerView children in Android?

I'm trying to set gravity for children in RecyclerView, but it looks like the LayoutParams does not have a gravity method. I have tried the following:

RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(
                RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT);

I have also tried the following:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

In both cases params.setMargin(int, int, int, int) works just fine and the margin is set correctly.

My RecyclerView:

<android.support.v7.widget.RecyclerView
    android:id="@+id/messages_list_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    android:layout_marginTop="20dp"/>

My RelativeLayout (child of RecyclerView):

<RelativeLayout
android:id="@+id/message_text_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:background="@drawable/chat_bubble"
android:layout_margin="16dp"
android:padding="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textColor="@android:color/primary_text_light"
        android:textSize="18sp"
        />

</RelativeLayout>

What I want to be able to do is params.setGravity(int) or any other hack that makes this possible. Thanks.

Upvotes: 5

Views: 3612

Answers (3)

Maasaivatar
Maasaivatar

Reputation: 141

This worked for me using Java inside the onBindViewHolder where I set properties for the TextView

`mTextView.setGravity(Gravity.CENTER_HORIZONTAL);`

Android Studio will inform you that the property for Gravity.CENTER_HORIZONTAL = Int 1 so you can enter the integer instead.

Other properties can be
Gravity.CENTER = Int 17
Gravity.RIGHT = Int 5
Gravity.LEFT = Int 3
Gravity.TOP = Int 48
Gravity.BOTTOM = Int 80

et-cetera

Upvotes: 0

ElegyD
ElegyD

Reputation: 4785

Wrap your item view inside a FrameLayout, make sure its layout_width is set to match_parent.

Give your layout which you wrapped in the FrameLayout an id and connect it to your ViewHolder. (I think you've already done this with @+id/message_text_wrapper)

Then in onBindViewHolder do this:

FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holder.yourLayout.getLayoutParams()
if (condition) {
    params.gravity = Gravity.END // or Gravity.RIGHT
} else {
    params.gravity = Gravity.START // or Gravity.LEFT
}

Upvotes: 1

kris larson
kris larson

Reputation: 30985

I would wrap the RelativeLayout in a FrameLayout with android:width="match_parent" and then set the gravity left/right on the FrameLayout.

Upvotes: 0

Related Questions