Search code examples
androidandroid-layoutlayoutandroid-viewandroid-api-levels

Layout-start-end,left-right concepts


I am little confused regarding usage of below attributes.

> android:layout_marginRight and   android:layout_marginEnd
> android:layout_marginLeft and android:layout_marginStart
> android:layout_toLeftOf and android:layout_toStartOf
> android:layout_toRightOf and android:layout_toEndOf
> android:layout_gravity="right|end"
> android:layout_gravity="left|start"

Below are the some of facts which i have mentioned.Please correct me if i am wrong.

  • To support both Ltr and Rtl layouts and to support versions prior to api level 17 is it advisable to put the above attributes always in pairs..

  • The "start" and "end" concepts were added in API Level 17 and will
    take precedence for Ltr device with api level>=17 and "right" and "left" concepts will take precedence for Ltr device with api level<17

  • If our app minskdversion is >=17 then we can ignore "right" and
    "left" attributes and use "start" and "end" attributes only

  • The "start" and "end" concepts will take precedence in all Rtl devices

Also I would like to know if there is any adverse effect in terms of performance/optimization if i add above attributes in pairs like-

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="right|end"
     android:layout_marginRight="@dimen/dp10"
     android:layout_marginEnd="@dimen/dp10"
     android:layout_marginLeft="@dimen/dp10"
     android:layout_marginStart="@dimen/dp10"
     android:layout_toLeftOf="@+id/bar"
     android:layout_toStartOf="@+id/bar"
     />

Solution

  • To support RTL in your apps you should:

    • If your app API level >=17 you should use “start” and “end” instead of “left” and “right (for example: layout_marginStart)
    • If your app API level <17 then you should add “start” and end” in addition to “left” and “right”. In other words - use both layout_marginRight and layout_marginEnd In other words any of your view should look like this: android:id="@+id/textView"

      android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginStart="16dp"
          android:layout_marginTop="20dp"
          android:text="@string/text_Field"
          android:textSize="24sp"
          android:textStyle="bold"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/imageView" />
      

      Note: android:layout_marginStart="16dp"

    Source