Search code examples
androidtextviewright-to-leftleft-to-right

How to set gravity for each line in android textview?


I have a String that contains English and Persian. I want if a line starts with Persian charachter then this line is displaied right to left in text view and if a line starts with English Character then this line is displaied left to right.

Note: I want to set gravity/direction for each line in text view.

How to implement it in android textview.

this is my textview xml code:

    <TextView
        android:id="@+id/textViewContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:autoLink="web"
        android:layoutDirection="rtl"
        android:gravity="start"
        android:layout_gravity="start"
        android:text="My Text"
        android:textColor="#000"
        android:textDirection="rtl" />

Solution

  • You can use the attribute of TextView to align text Content.

    Allign text to left in the layout

    android:gravity="start"
    

    Aliign text to right in the layout.

    android:gravity="end"