Android localization for RTL languages - android

I need to add localization features to my app for RTL languages (hebrew and arabic)
How can I modify the layout in order for the picture to be aligned differently in the RTL option?
For example: in the main menu I got buttons on the left side and pic on the right (in the LTR version) I need to reverse it and not only change the pictures

find the localization label for RTL languages then save their custom layout xml files under the folder "res/layout-RtlLabel/customLayout.xml". For example "layout-fr"... look here for more info

you can use
android:layoutDirection="locale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Related

Support RTL for only one specific language

I have an app with translations in English and Arabic, so I go with android:supportsRtl="true" in AndroidManifest.xml.
The problem is that some users are using some RTL languages out of Arabic that has no translation in my app, e.g. Hebrew, so it shows the English translation with RTL.
How can I avoid that and keep RTL support only when the language is Arabic and other RTL languages' users will get the English translation with LTR?
Programatically, you can alter the layout direction for some languages.
if (lang in listOf(...))
theView.layoutDirection = Layout.DIR_RIGHT_TO_LEFT
Or, you can use android:layoutDirection attribute.
some_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layoutDirection="#integer/viewDirection"
...
Define default viewDirection in values/viewDirection.xml.
<integer name="viewDirection">0</string>
// 0 is for left to right
And also in the rtl language resource folder
values-ar/viewDirection.xml
<integer name="viewDirection">0</string>
// 1 is for right to left
You have to specify layout direction in all concerned views but some dialog's may still be in rtl mode.
It might be worth to check the documentation for LayoutInflater.Factory2.
Maybe, there is a much simplier way to solve this problem.

Localization text direction supportsrtl:false in arabic in android for string resources not working

I am trying to localize my app in arabic by using **
android:supportsRtl:false
** in my manifest file.It actually works for all the strings declared in the xml layout i.e **
android:text="#string/done_btn"
**and all the arabic localized strings are written ltr(left to Right) but those strings which are written programmatically using resources strings i.e
getString(R.id.name)
are localized but it is using it's default written type i.e rtl(Right to left) instead of ltr(left to Right) thereby destroying the layout and design of the page.
N.B:1> i already tried setting android:layoutDirection="ltr" or android:textDirection="ltr" to the textview with localized string or the container layout which it holds.
Any help would be highly appreciated.

Ltr for Persian locale in android

I am developing an app which supports two languages: English("en") and Persian("fa"). I have set android:supportsRtl="false" in AndroidManifest.xml since I need everything to be from left to right. I set margins for all views that I have but for the ones containing a string, it is not working right and it still seems like it is setting the directions from right to left. How can I fix that? I also tried changing the layoutDirection manually to left to right but that did not work either.
You must handle your locale in all activities. Android by default uses the locale of the device to select the appropriate language dependent resources.
Also you must consider project minimum sdk. I recommended to you change it to 16 or higher: minSdkVersion 16
Maybe this link help you.
I set margins for all views that I have but for the ones containing a string, it is not working right
For this for example you must use from android:layout_marginEnd instead of android:layout_marginRight. Also in default create your layout for en locale and then handle and change it by app locale to fa or another locale.
You have to use instead of the right from the end and instead of the left from the start
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="تست"
android:gravity="start"/>

Autoinsert right-to-left attributes Android Studio

I don't know all features of Android Studio, I have tried to search,but found nothing.
I wonder if it is possible to make Android Studio autoinsert attributes required for right-to-left support.
For example I have typed following line
android:layout_marginLeft="10dp"
Is it possible to make Android Studio insert marginStart attribute automatically ?
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
And vice-versa(for padding too).
This would save some time.
Maybe someone knows how to get such behavior, I will be grateful for any help. Thx.
Apparently, you can't do that. You have to write them yourself.
The difference between layout_marginLeft and layout_marginStart for example is that layout_marginLeft is executed for left to right languages like English, while `layout_marginStart' is only executed for right to left languages like Arabic.
So if you are going to add string translations to your app including arabic or any other right to left languages, you will need to write marginStart or marginEnd attributes... So anyway, if you didn't write layout_marginStart for example and your app doesn't support Arabic language or any other right to left languages, no error will occur to the user; it is just a warning that Android Studio tells you.
Definitely the same concept is applied to padding attributes. Hope that helps you.
Edit:
If you don't like to see Android Studio warning you these warnings, you can simply disable that by clicking on the yellow light bulb beside the yellow highlighted warning and selecting Edit 'Using left/right instead of start/end attributes' inspection settings, then uncheck it from the list.
But if you don't want to change the inspection settings, you can just add the following to your View that you don't want to use start/end attributes in it:
tools:ignore="RtlHardcoded"
and add that to your parent layout that contains that View:
xmlns:tools="http://schemas.android.com/tools"

Language Specific layout for android

I know that we can display multiple languages support for our android application with different
values folder example values-en , values-ar.
My question is can we change our layout style when there is change of language.
As in English every thing starts form "left -to right" however Arabic is just apposite of it .
so is it possible to place a image on left when language selected is English and Change the layout when language changes to Arabic
The layout direction of your application. ldrtl means "layout-direction-right-to-left". ldltr means "layout-direction-left-to-right" and is the default implicit value.
This can apply to any resource such as layouts, drawables, or values.
For example, if you want to provide some specific layout for the Arabic language and some generic layout for any other "right-to-left" language (like Persian or Hebrew) then you would have:
res/
layout/
main.xml (Default layout)
layout-ar/
main.xml (Specific layout for Arabic)
layout-ldrtl/
main.xml (Any "right-to-left" language, except
for Arabic, because the "ar" language qualifier
has a higher precedence.)
Note: To enable right-to-left layout features for your app, you must set supportsRtl to "true" and set targetSdkVersion to 17 or higher.
can we change our layout style when there is change of language.
Yes. You can provide different layouts according to the language user chooses. This is clearly described in the Providing Resources documentation.
Infact a specific layout qualifier is provided for supporting right-to-left-directed languages called res/layout-ldrtl.
P.S: This attribute is only supported from API 17.
Add direction- and language-specific resources
This step involves adding specific versions of your layout, drawables, and values resource files that contain customized values for different languages and text directions.
In Android 4.2 (API level 17) and higher, you can use the -ldrtl (layout-direction-right-to-left) and -ldltr (layout-direction-left-to-right) resource qualifiers. To maintain backward compatibility with loading existing resources, older versions of Android use a resource's language qualifiers to infer the correct text direction.
Suppose that you want to add a specific layout file to support RTL scripts, such as the Hebrew, Arabic, and Persian languages. To do this, you add a layout-ldrtl/ directory in your res/ directory, as shown in the following example:
res/
layout/
main.xml //This layout file is loaded by default.
layout-ldrtl/
main.xml //This layout file is loaded for languages using an
//RTL text direction, including Arabic, Persian, and Hebrew.
If you want to add a specific version of the layout that is designed for only Arabic text, your directory structure becomes the following:
res/
layout/
main.xml //This layout file is loaded by default.
layout-ar/
main.xml //This layout file is loaded for Arabic text.
layout-ldrtl/
main.xml //This layout file is loaded only for non-Arabic
//languages that use an RTL text direction.
Note: Language-specific resources take precedence over layout-direction-specific resources, which take precedence over the default resources.

Categories

Resources