Search code examples
androidtextviewhebrew

RTL only works with multiple lines


I've got an Android app, which can read RSS feeds. I've got RTL supported in my manifest. When I display an RSS feed which is in Hebrew / Arab with RTL something remarkable happens.

image
The image shows how the text in the textview (in the list) correctly aligns long text, that takes up 2+ lines, right. However, 1 line text is not correctly aligned. All text is parsed and set to the textview the same way.

This is my textview style:

    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:lineSpacingExtra">1dp</item>
    <item name="android:paddingLeft">5dp</item>
    <item name="android:paddingTop">2dp</item>
    <item name="android:maxLines">2</item>
    <item name="android:ellipsize">end</item>
    <item name="android:textColor">@color/dark_gray</item>
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>

What causes this difference? And how can I make sure all (hebrew/arab) text is aligned right.


Solution

  • When you use a layout_width of wrap_content, the width of your view adjusts based on how much content exists. In the case of short strings, that means that the right edge of your view is right at the edge of the text, rather than tied to the right edge of your FrameLayout. This causes even the right aligned text to appear left aligned.

    Instead, you should fix both edges of the TextView - you already handle the left edge via android:layout_toRightOf - add android:layout_alignParentRight="true" as well so that the rightmost edge of the view will always correspond with the rightmost edge of the FrameLayout.