Kamran Sohail
Kamran Sohail

Reputation: 622

How to position Label from right to left instead of left to right according to language selected?

I have a multi language application. which works fine for the app is in the English language and all labels are placed perfectly at the required places.

Now I have a scenario when I change the language of the from English to Arabic app is doing the translation correctly. But issue is with the placement of the labels, I want to place the labels from rtl instead of ltr.

Upvotes: 4

Views: 621

Answers (3)

Matthieu Meunier
Matthieu Meunier

Reputation: 473

In addition to @Ankita's answer you can use textDirection:"anyRtl", which will set the direction of the text to the right if it finds any strong "right direction" character or to the left he it finds a strong "left direction" character

From Documentation of views

Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains any strong RTL character, otherwise it is LTR if it contains any strong LTR characters. If there are neither, the paragraph direction is the view's resolved layout direction.

I would recommend making 2 different layouts for each view one with left direction and one with right direction. In that case you will have the liberty to adapt your layout to the type of text you're going to have and so use textDirection="rtl"

Either cases are good, the first one from Ankita is useful when you don't need full control of the display and you can permit yourself to trust the "anyRTL" algorythm.

The second solution, gives you more work but will give you full control over the result layout.

One last thing. I'm not sure but by creating a layout with the arabic locale I think the text direction changes automatically

Right ot Left or Left to right layout

Upvotes: 1

Dentor
Dentor

Reputation: 600

In order to support RTL in your app, you first need to add android:supportsRtl="true" to the <application> element in your manifest file.

If your app API ≥ 17, replace all

layout_marginLeft with layout_marginStart

layout_marginRight with layout_marginEnd

paddingLeft with paddingStart

paddingRight with paddingEnd

and if your app support API<17 add layout_marginStart, layout_marginEnd along with layout_marginLeft, layout_marginRight

Upvotes: 1

Ankita
Ankita

Reputation: 1149

For API level 17 or higher Set the property textDirection in your XML

android:textDirection="anyRtl"

For below API level 17 use property textAlignment in your XML

android:textAlignment="viewStart"

Upvotes: 3

Related Questions