Layout changes if device setting is force RTL - android

How to override Android option RTL(Force RTL layout direction) to keep an app LTR. I have Tried to set an option in manifest.xml but it did not work
android:layoutDirection="ltr"
android:supportsRtl="false"
tools:replace="android:supportsRtl"

Related

Align horizontal LinearLayout from left to right [duplicate]

I've made an app, I've tested it and it was fine on my phone. But... when I gave the .apk to someone else whose phone language is RTL the whole layout broke and it messed up everything.
My question is - How can I force my app to use only LTR and disable the auto layout change which breaks my whole app design?
In your manifest file and inside the application tag add these two lines.
<manifest>
<application
.
.
.
android:supportsRtl="false"
tools:replace="android:supportsRtl" //(replace libraries' Rtl support with ours)
>
</application>
</manifest>
Note: (about second line)
Some libraries have support Rtl in their manifest file so if you want to use those libraries you must replace their manifest line of code with yours.
Android 4.2 added full native support for RTL layouts. To take advantage of RTL layout mirroring, simply make the following changes to your app:
Declare in your app manifest that your app supports RTL mirroring.
Specifically, add android:supportsRtl="true" to the
element in your manifest file.
Change all of your app's "left/right" layout properties to new
"start/end" equivalents. If you are targeting your app to Android 4.2
(the app's targetSdkVersion or minSdkVersion is 17 or higher), then
you should use “start” and “end” instead of “left” and “right”. For
example, android:paddingLeft should become android:paddingStart. If
you want your app to work with versions earlier than Android 4.2 (the
app's targetSdkVersion or minSdkVersion is 16 or less), then you
should add “start” and end” in addition to “left” and “right”. For
example, you’d use both android:paddingLeft and android:paddingStart.
For more precise control over your app UI in both LTR and RTL mode, Android 4.2 includes the following new APIs to help manage View components:
android:layoutDirection — attribute for setting the direction of a
component's layout.
android:textDirection — attribute for setting the
direction of a component's text.
android:textAlignment — attribute
for setting the alignment of a component's text.
getLayoutDirectionFromLocale() — method for getting the
Locale-specified direction
-- Source & Credits --
Just add the following to the manifest
android:supportsRtl="false"
tools:replace="android:supportsRtl"
Just add something like this in your App Theme Style:
<item name="android:layoutDirection">ltr</item>
or
<item name="android:layoutDirection">rtl</item>
For React Native adding the following line in the manifest file was enough:
// android/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kidspodmobileclient">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:supportsRtl="false" // <== add this line here
It would be like the following screenshot:
RN version: 0.68.2

How to use layout_gravity="start" either layout_gravity="LEFT" in Navigation view Programatically in Android

My problem is that when the language is Arabic then i want to DrawerLayout open with the gravity of RIGHT and when the language will English then to DrawerLayout open with the gravity of Left.......
You can use end and start keywords instead of left and right, to achieve this
like android:layout_gravity="end"
Also, add this to your manifest > application android:supportsRtl="true"

How to force rtl support for a view?

I've disabled and forced my application to not support rtl . Because it made lots of problems of views and layouts , this is my manifest code:
<application
android:name="ir.dizbon.persiandesigners.ParseApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="false"
android:theme="#style/AppTheme"
tools:replace="android:supportsRtl">
in one layout I add this code :
android:layoutDirection="rtl"
When I remove the manifest code ,the layout change the direction and works fine but I can't remove manifest codes .
I tried to change the direction pragmatically:
pass_l.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
it doesn't work either .
How can I make my view to support rtl and get rtl ?
You can create different layout in android studio to support rtl.
Right click layout -> New -> Layout Resource File -> then choose Layout Direction Qualifier.

Android - Wrong layout direction when switching RTL vs LTR many times

When I switch my app language between EN and AR (during runtime) the views behave correctly by moving from the LTR to the RTL but when I start stressing the app by switching languages many times the layout direction become missy and I get RTL elements when the app is supposed to show LTR elements. Sometimes the layout direction is not refreshed anymore.
As workaround I have to set the layout direction programmatically for those views. binding.spinner.setLayoutDirection(LocaleUtil.isRtl(this) ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR);
Do you have any idea please ? I am trying to avoid this kind of workaround :( If it is possible to rely entirely on the Android UI rendering.
I can't imagine a user who will switch language many times and then returning to app on every change :)
But, anyway, if Android is not able to recreate your activities correctly you always can do it manually by setting "android:configChanges" in AndroidManifest.xml for your activities and then listening for onConfigurationChanged(Configuration newConfig) method in such activities:
In AndroidManifest.xml:
<activity android:name=".MyActivity"
android:configChanges="layoutDirection">
In Activity for which you set android:configChanges:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
// change directions for you views to RTL
} else {
// change directions for you views to LTR
}
}
make sure that supportsRtl="true"
<application
android:supportsRtl="true"
android:name=".App"
android:icon="#drawable/news"
android:label="#string/app_name"
android:theme="#style/AppTheme">
</application>

How to override RTL support on a layout in Android

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.

Categories

Resources