title: How to detemine the current direction of a View (RTL/LTR)?
body: <h2>Background</h2>
  
  <p>It's possible to get the current locale direction, using this:</p>
  
  <pre><code>val isRtl=TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL
  </code></pre>
  
  <p>It's also possible to get the <a href="https://developer.android.com/reference/android/util/LayoutDirection.html" rel="noreferrer"><strong>layout direction</strong></a> of a view, if the developer has set it:</p>
  
  <pre><code>val layoutDirection = ViewCompat.getLayoutDirection(someView)
  </code></pre>
  
  <h2>The problem</h2>
  
  <p>The default layoutDirection of a view isn't based on its locale. It's actually <a href="https://developer.android.com/reference/android/view/View.html#LAYOUT_DIRECTION_LTR" rel="noreferrer"><strong>LAYOUT_DIRECTION_LTR</strong></a> .</p>
  
  <p>When you change the locale of the device from LTR (Left-To-Right) locale (like English) to RTL (Right-To-Left) locale (like Arabic or Hebrew) , the views will get aligned accordingly, yet the values you get by default of the views will stay LTR...</p>
  
  <p>This means that given a view, I don't see how it's possible to determine the correct direction it will go by.</p>
  
  <h2>What I've tried</h2>
  
  <p>I've made a simple POC. It has a LinearLayout with a TextView:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      android:id="@+id/linearLayout" xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent" android:gravity="center_vertical" tools:context=".MainActivity"&gt;
  
      &lt;TextView
          android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content"
          android:text="Hello World!"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>In code, I write the direction of the locale, and of the views:</p>
  
  <pre><code>class MainActivity : AppCompatActivity() {
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          val isRtl = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL
          Log.d("AppLog", "locale direction:isRTL? $isRtl")
          Log.d("AppLog", "linearLayout direction:${layoutDirectionValueToStr(ViewCompat.getLayoutDirection(linearLayout))}")
          Log.d("AppLog", "textView direction:${layoutDirectionValueToStr(ViewCompat.getLayoutDirection(textView))}")
      }
  
      fun layoutDirectionValueToStr(layoutDirection: Int): String =
              when (layoutDirection) {
                  ViewCompat.LAYOUT_DIRECTION_INHERIT -&gt; "LAYOUT_DIRECTION_INHERIT"
                  ViewCompat.LAYOUT_DIRECTION_LOCALE -&gt; "LAYOUT_DIRECTION_LOCALE"
                  ViewCompat.LAYOUT_DIRECTION_LTR -&gt; "LAYOUT_DIRECTION_LTR"
                  ViewCompat.LAYOUT_DIRECTION_RTL -&gt; "LAYOUT_DIRECTION_RTL"
                  else -&gt; "unknown"
              }
  }
  </code></pre>
  
  <p>The result is that even when I switch to RTL locale (Hebrew - עברית), it prints this in logs:</p>
  
  <pre><code>locale direction:isRTL? true 
  linearLayout direction:LAYOUT_DIRECTION_LTR 
  textView direction:LAYOUT_DIRECTION_LTR
  </code></pre>
  
  <p>And of course, the textView is aligned to the correct side, according to the current locale:</p>
  
  <p><a href="https://i.stack.imgur.com/JNOGC.png" rel="noreferrer"><img src="https://i.stack.imgur.com/JNOGC.png" alt="enter image description here"></a></p>
  
  <p>If it would have worked as I would imagine (meaning LAYOUT_DIRECTION_LOCALE by deafult), this code would have checked if a view is in RTL or not:</p>
  
  <pre><code>fun isRTL(v: View): Boolean = when (ViewCompat.getLayoutDirection(v)) {
      View.LAYOUT_DIRECTION_RTL -&gt; true
      View.LAYOUT_DIRECTION_INHERIT -&gt; isRTL(v.parent as View)
      View.LAYOUT_DIRECTION_LTR -&gt; false
      View.LAYOUT_DIRECTION_LOCALE -&gt; TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL
      else -&gt; false
  }
  </code></pre>
  
  <p>But it can't, because LTR is the default one, and yet it doesn't even matter...</p>
  
  <p>So this code is wrong.</p>
  
  <h2>The questions</h2>
  
  <ol>
  <li><p>How could it be that by default, the direction is LTR, yet in practice it gets aligned to the right, in case the locale has changed?</p></li>
  <li><p>How can I check if a given View's direction would be LTR or RTL , no matter what the developer has set (or not set) for it ?</p></li>
  </ol>