1

I am making chat app and I want to align text message to right side of parrent (which in that case is LinearLayout).

Here is what I get: picture

And here is what I want: picture

And here is my XML code for this layout:

            <LinearLayout
        android:id="@+id/chatMessages"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/background2"
            android:text="Losowy tekst"
            android:textSize="25dp"></TextView>


    </LinearLayout>

So please help me, how can I do it.

Igorson
  • 51
  • 1
  • 4
  • Does this answer your question? [How to set layout\_gravity programmatically?](https://stackoverflow.com/questions/8049620/how-to-set-layout-gravity-programmatically) – javdromero Aug 05 '21 at 15:30

2 Answers2

1

On your TextView, use android:layout_gravity="end"

javdromero
  • 1,850
  • 2
  • 11
  • 19
0

please try this

LayoutParams la= new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); la.gravity = Gravity.END;