2

Background

I was tasked to work on an app that has android:supportsRtl="false" in its manifest. Not sure why it was set as such, but it seems that in some places the user can choose the direction. Also in some cases it's floating (using system-alert-window permission).

The problem

I wanted to have some ConstraintLayout that I will be able to change its direction at runtime, as indeed in this app I have to let the user choose the direction.

For this, I made a small POC, combining all basic cases that I could find, to make sure all will be affected:

<androidx.constraintlayout.widget.ConstraintLayout
    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:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="bias=0"
        app:layout_constraintBottom_toTopOf="@id/textView2" app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="aligned to start alone"
        app:layout_constraintBottom_toTopOf="@id/textView3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView1" />

    <TextView
        android:id="@+id/textView3" android:layout_width="0px" android:layout_height="wrap_content"
        android:text="take whole row" app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView2" />

</androidx.constraintlayout.widget.ConstraintLayout>

On LTR, they are all aligned to the left, as expected.

The problem here is that with or without trying to change the direction of the layout, it seems to be affecting only the first 2 cases (and worse in case of the flag being set to false, as it causes nothing to occur).

What I've tried

First I wanted to see how it works without setting the flag of android:supportsRtl="false". I tried changing the layout direction of the ContstraintLayout, and sadly it affected only 2 of the TextViews:

val container = findViewById<ConstraintLayout>(R.id.container)
container.layoutDirection = View.LAYOUT_DIRECTION_RTL

enter image description here

This occurs when changing the locale to RTL language too (Hebrew for example).

Adding the flag, as I wrote, made them all be as if it's still on LTR : All were aligned to the left.

I also tried to call container.requestLayout() . It didn't help.

I want to work only on this layout, without changing the behavior for other places, which could cause visual bugs there. So I don't want to change the flag (I've already noticed it causes many issues when the locale of the device is set to RTL language).

The questions

  1. How come only for 2 cases it gets affected by the direction? How can I let the other be affected too?
  2. How can I force the direction to be changed even if the flag is set (android:supportsRtl="false") ? I want it to work just for this layout (at least for now), and not affecting anywhere on the app.
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • I'd like to add that for LTR/RTL to "have an effect" the constraints *must use* `start/end` and not `left/right`, otherwise it's "hardcoded" and will *not move*. This doesn't explain why the last TextView (aligned to "end" did not move). I think you're going to need to provide a bit more context. – Martin Marconcini Aug 18 '21 at 08:35
  • @MartinMarconcini Correct. I've updated the first case and it works as you wrote. Only the last one remains, but I also can't work with this at all when `android:supportsRtl="false"` is being set. How can I stay with this flag? I don't want to affect anything in the rest of the app... – android developer Aug 18 '21 at 08:54

0 Answers0