Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I force textView containing ltr characters, to right aligned on rtl enviroment

all "lines" uses the same struct (width, height, gravity, ...:

<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="30dp"
  android:orientation="horizontal">

  <TextView
     android:id="@+id/labelMkt1"
     android:layout_width="0dp"
     android:layout_height="30dp"
     android:layout_weight="0.5"
     android:gravity="center_vertical"   // I also tried "center_vertical|start"
     android:text="@string/report_mkt"                       
     android:textColor="#000" /> <!-- force gravity start, textAlignment=textStart, did not work, useful for RTL layout with EN characters -->

  <TextView
    android:id="@+id/labelMkt2"
    android:layout_width="0dp"
    android:layout_height="30dp"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:text="@string/device_param_empty" />
</LinearLayout>

but when label1 contains English characters (e.g. "MKT") , the text is aligned left.

mkt is left aligned

  • I tried to use android:gravity="center_vertical|start" (although "start" is the default) and it did not help.

  • I tried to add android:textAlignment="textStart" and it did not help.

How can I force label1 to align right, as all other labels?

like image 780
Atara Avatar asked Mar 12 '19 12:03

Atara


People also ask

How do I align text to the right view?

To right align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “viewEnd” in layout file, or programmatically set the textAlignment property of the TextView object with View.

How do I make a TextView look like a link?

This is the shortest solution: final CharSequence text = tv. getText(); final SpannableString spannableString = new SpannableString( text ); spannableString. setSpan(new URLSpan(""), 0, spannableString.


2 Answers

You can use android:textDirection="rtl" so no matter if the text is in Hebrew or English it will start from the right side of your view and won't jump to the other direction when you change the language.

Note - I think that android:gravity didn't work for you because Hebrew is written rtl and English is written ltr.
so when you put gravity - start and the language is English your text will start from the left side of your view.

Edit with a working example:

Layout:

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<TextView
    android:id="@+id/labelMkt1"
    android:layout_width="0dp"
    android:layout_height="30dp"
    android:layout_weight="0.5"
    android:gravity="center_vertical"
    android:text="@string/report_mkt"
    android:textColor="#000"
    android:background="@color/colorPrimaryDark"
    android:textDirection="rtl" />

<TextView
    android:id="@+id/labelMkt2"
    android:layout_width="0dp"
    android:layout_height="30dp"
    android:layout_weight="1"
    android:background="@color/colorAccent"
    android:gravity="center_vertical"
    android:text="@string/device_param_empty"
    android:textDirection="rtl" />

String resources:

<resources>
<string name="app_name">My Application</string>
<string name="report_mkt">this is english text</string>
<string name="device_param_empty">this is english text</string>

How it looks on a real phone:

enter image description here

So as you can see I too got my strings in English and everything is working fine, so the problem is not from having English strings.

like image 92
Tamir Abutbul Avatar answered Nov 14 '22 23:11

Tamir Abutbul


Use

android:textAlignment="viewEnd"

and text will be aligned regardless of its content, but still depended on the layout direction.

The Documentation here:

Align to the end of the view, which is ALIGN_RIGHT if the view's resolved layoutDirection is LTR, and ALIGN_LEFT otherwise.

like image 21
Josef Grunig Avatar answered Nov 15 '22 00:11

Josef Grunig