2

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" />
Hamidreza Shokouhi
  • 973
  • 1
  • 12
  • 24
  • Possible duplicate of [Align textview according to the language selection (LEFT -RIGHT)](http://stackoverflow.com/questions/14728604/align-textview-according-to-the-language-selection-left-right) – Amit Vaghela Jul 05 '16 at 12:13
  • @AmitVaghela I updated my tittle and explanation about my problem. – Hamidreza Shokouhi Jul 05 '16 at 12:25
  • Can you split your text and show English and Persian text parts separately in own `TextViews`? It seems to be the easiest solution IMHO. – artkoenig Jul 05 '16 at 12:41
  • @Artjom I use this text view in recycle view, and i think this solution reduce recycle view perforamnce. what's your idea about this? – Hamidreza Shokouhi Jul 05 '16 at 13:09
  • 1
    I would always prefere an easy solution and optimize it only if it is **significantly** affects the performance. In your case I don't think that using multiple TextViews will have noticeable performance drawback. – artkoenig Jul 05 '16 at 13:17

5 Answers5

5

What you are looking for is ANY_RTL text direction to be added to your text view

    android:textDirection="anyRtl"

And you will get a result like this:

enter image description here

Conside that it is an API17+ solution.

Community
  • 1
  • 1
EmJiHash
  • 623
  • 9
  • 22
2

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"
Aman Jain
  • 2,975
  • 1
  • 20
  • 35
0

Use the gravity attribute.

Instead of left/right use start, that was added for rtl support.

https://developer.android.com/reference/android/widget/TextView.html#attr_android:gravity

user6547359
  • 204
  • 1
  • 9
0

use like this

           if (textview.contentEquals("persian")) {
                textview.setGravity(Gravity.START);
            } else if (textview.contentEquals("english")) {
                textview.setGravity(Gravity.END);
            }
Sagar Chavada
  • 5,169
  • 7
  • 40
  • 67
0

I guess you can try to use spannable and the Alignment properties:

Alignment.ALIGN_OPPOSITE (right) and Alignment.ALIGN_NORMAL (left).

Check this answer: Multiple alignment in TextView?

Community
  • 1
  • 1