0

I have such item xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/messageLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:padding="5dp">


    <TextView
        android:id="@+id/newDayTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:visibility="gone" />


    <TextView
        android:id="@+id/messageData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/newDayTv"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp" />

    <TextView
        android:id="@+id/messageText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/messageData"
        android:layout_margin="10dp" />


</RelativeLayout>

and I would like to change its alignment like layout_alignParentEnd and similars one. I saw this question but as you can see at this question TS used two layouts, and maybe it will be possible to use one xml and only change its gravity? I also saw this question but I didn't manage to solve my problem.

Andrew
  • 1,947
  • 2
  • 23
  • 61

1 Answers1

0

Java

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.END;

messageLayout.setLayoutParams(params);

Kotlin

val params = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        ).apply {
            gravity = Gravity.END
        }

messageLayout.layoutParams = params 

I hope it's helpful for you!

Sanjay Chauhan
  • 893
  • 7
  • 13