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.
Related
I have an app that usually translates into 3 different languages. Thai, indonesia and english (default). but on occasion i have a feature that is only applicable to thailand and english and indonesia does not need translations so i have left indonesia local string file empty.
Lets take a look at an example using android studios strings translations editor:
as you can see these a1,a2,a3,a4 are only required in thailand and english (default). so i left indonesia empty. Now lint gives off warnings that i am missing translations for indonesia. Is there any flag i can use to stop the lint check or is the best practice to still copy the string into indonesia even though its not used ?
There's no way to specify what you want exactly since your use case is I guess not widely used and it didn't make it into lint.
You can however use the tools:ignore="MissingTranslation"
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="foo" tools:ignore="MissingTranslation">1</string>
</resources>
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"/>
I set android:supportsRtl="true" in the <application> tag in AndroidManifest.xml, but I need to force one of the views to be left-to-right nonetheless, even when the language of the interface is Hebrew or Arabic. How can I force a specific view to be LTR in an RTL application?
Specifically, I want to force some linear layout to go left-to-right instead of the default right-to-left even when the language is right-to-left.
Generally gravity="left" is enough to force text to be left-to-right. But it didn't help with the direction of a linear layout. The solution was to add android:layoutDirection="ltr".
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="ltr">
for letf to right all layout content.
To complete the answers, aside from XML, layout direction can also be changed programmatically with ViewCompat.setLayoutDirection(view, LayoutDirection.RTL). This API can be used from API 19 and onwards, so If your min sdk version supports API below 19, an if-check needs to be performed:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ViewCompat.setLayoutDirection(...)
If you want to force your application to ltr even on rtl languages you don't need layout direction (it works on api>17) you can just set android:supportsRtl to false.
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.
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"