Language Specific layout for android - 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.

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.

Make changes to all layout files at once in android

I know that we can have different layout files for supporting different screen sizes in Android.
Does anyone know if there is an option to change all other layout files when I make changes to the original layout file? For example, say I have a layout file - main.xml under layout, layout-large, layout-sw600dp and layout-sw720dp directories. If I make some changes to the main.xml in the layout directory, is there any setting which would automatically make that change in the other layout directories as well?
For the formatting capabilities I use an answer for this. This refers to the comment above and elaborates on Aleksey's answer.
<include
android:id="#+id/some_id_if_you_nee_one"
layout="#layout/some_other_xml_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
You may add any additional (orientation or resolution specific) formatting like below the layout= statment.
Everything that is not specific to the current resolution/orientation and common for all resolutions/oriantations should go into some_other_xml_file.xml
This works for all full views and subclasses of view. If you want something similar only for groups of style attributes then you can start a styles.xml and refer to the styles with style="..." statements.
The answer is no. And i'm not sure there is such tool. Exceptions: naming of params (strings, drawables, etc.).
Some hint: compose layouts from small parts, that inserted using
<include />.
So when you change small parts – all layout changes (not sure with different sw-*** layouts, but in one folder it works).

How to use tools:locale for strings.xml files?

Background
Lint has a relatively new feature, so that it will warn us about missing translation only for languages that we choose, but i don't get how to use it.
The problem
for some reason, Lint still warns me about languages that i don't intend on translating yet.
What i've tried
for example, currently i want to only have 2 languages : english ("en") and hebrew (which is sadly both "iw" and "he" ) .
so i have strings files in the folders :
values (for english)
"values-he" and "values-iw" (for hebrew) .
i've tried putting the new attribute in the english file as such :
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en,iw,he">
...
The question
what is the right way to do it?
Looking here it seems that it's to be used into resource files to indicate the default language. So you can specify only one locale code.
should correspond to a language
Moreover it seems to be used only to disable spell-checker
If you read the article:
This lets you tell the tools which language you're using in your
base values folder. For strings in for example values-de or values-en it's obvious, but not in the base "values" folder
It need only to know what is the language in the default "values" folder (the folder without any attribute).
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en">
You are already in right direction. Just need some modification. Like this manner:
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">
Now we know that the language used for strings in the default values folder is Spanish rather than English.
Used by: Lint, Studio (to disable spell checking in non-English resource files)
Reference Link: Go to here tools:locale
Thanks.

change textSize with different language locale

I have added spanish and french to my app but some of the wording is longer in spanish then english. how can i change the textsize when the values-es/string.xml file is accessed
You can use the dimens.xml resource file for this purpose. In your case you'll probably want to create a file called res/values-es/dimens.xml, and possibly also a -fr version. You can specifify the default values in res/values/dimens.xml (or res/values-en/dimens.xml, if you want to be more specific).
Example grabbed from the More Resource Types section on developer.android.com:
dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_height">25dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="ball_radius">30dp</dimen>
<dimen name="font_size">16sp</dimen>
</resources>
Apply in xml
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
Or in code
float fontSize = getResources().getDimension(R.dimen.font_size);
There are also solutions here on SO that use a iterative/recursive process to shrink the text size of a TextView to 'fit' in its bounding box (using a custom view), but I'd say above is a more robust approach, especially if you're considering adding more languages in the future.
The above explanations are correct, but they don't fully explain how to do it.
When you open your project in Android Studio, then it automatically shows this project in the "Android" mode. You need to click on the "Android" tab in the top, left corner of Android Studio and select "Project". Then you need to go into "app>src>main>res". Then you need to right-click on the "res" folder and from the menu that comes up, select "New>Android resource directory". A dialogue will come up, and for Directory name: type in values-es and click OK.
This will create a folder for all Spanish Locale values. And then you can right-click on this values-es folder to create dimens.xml, string.xml, color.xml, ...etc. files that will be used whenever Spanish Locale is selected in the phone.
If you've already created a string.xml file for Spanish Locale through the graphical user interface, then the values-es folder with string.xml file will already be in the Project, when you go there. And in this case, you just need to right-click on the values-es folder to create the dimens.xml file there for Spanish Locale.
You would need to specify a different layout file in layout-es. That way when Android pulls from values-es/string.xml, it'll load the different layout-es/yourfile.xml. That layout file can then specify a theme, style, or text size on the views.

Android localization for RTL languages

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"

Categories

Resources