1

I've been recently looking into supporting RTL text and layouts. According to this post android will natively support and switch layoutDirection automatically for API-17, which is why the Start and End gravities were added. The issue now is how to support this in a similar way for pre API-17.

This question has been asked many times before, with the general solution being to inspect the locale or text, then set gravity as needed. At least it would require less effort than a solution like this.

The reason I'm revisiting this question now is because I have noticed an update to support-v7-appcompat which now contains classes such as AppCompatTextView and LinearLayoutCompat. As far as I know, the point of these support libraries is to mimic the default behavior of later Android releases. I created a layout to test this.

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#80ccffee">
<android.support.v7.widget.AppCompatTextView
      android:id="@+id/textview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentEnd="true" />
</RelativeLayout>

When I give textview an arabic string, it is correctly detected as RTL text and automatically right-aligned on an API-17 device, where as the same does not happen on an API-10 device. It's possible that I misunderstood the purpose of AppCompatTextView, but the presence of GravityCompat.START suggests to me that its intended as a workaround.

I then wrapped the textview in a LinearLayoutCompat instead in the hopes that the layout's direction will influence it, but no luck.

So my question is: is there a mechanism in the new AppCompat update which can be used to support RTL without having to set each TextView's gravity in code?

Community
  • 1
  • 1
Kevin
  • 1,626
  • 16
  • 30

1 Answers1

0

Yes, there is!

You can achieve what you want by overriding default attributes of theme as described below:

Go to res -> values -> styles.xml

override attributes like this:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <item name="android:textDirection">rtl</item>
    <item name="android:gravity">start</item>
</style>
Sajjad
  • 430
  • 6
  • 17