mozhi jafr
mozhi jafr

Reputation: 77

What's the difference between setGravity=right and setLayoutDirection=rtl in a layout

When I want to change the direction of layout to right if I assign android:gravity="right" I will give the same result as when I assign android:layoutDirection="rtl".

I want to know the difference between two of them.

Upvotes: 1

Views: 969

Answers (2)

Dr. Nitpick
Dr. Nitpick

Reputation: 1712

rtl means "right to left", you would use android:layoutDirection="rtl" if you wanted a view group to display content specifically for right to left languages (always) like Arabic. This is why it is aligning your view to the right. That said, I would recommend that you shy away from ever using layoutDirection since it overrides whatever the locale of the device is.

android:gravity="right" will align a view to the right. I also recommend that you don't use this (or gravity="left") because it will not adjust for rtl languages, so if you ever release an Arabic version of the app the layout will look odd for your Arabic users.

Instead you should use android:gravity="end" as this will align your view right in ltr languages (English) and left in rtl languages (Arabic). You need to have a minSDK>17 to use end and start alignments, but I am assuming you are because you also need a min sdk of 17 to use layoutDirection.

Upvotes: 1

Murli Prajapati
Murli Prajapati

Reputation: 9733

This is RTL(Right to left) Layout. Everything is aligned from right to left for those who know Urdu or Arabic.

Simple RTL Layout
RTL layout for localization

layout_gravity = "right" align your view to right of parent. Refer the image. Only TextView is aligned to right and everything else is left to right.

enter image description here

Upvotes: 0

Related Questions