I've set my application to support RTL by adding the "supportRTL = true" to the Android Manifest. Now I Want to create an activity with a LTR orientation, meaning the Toolbar should appear LTR and not RTL. Problem is, whatever I do, I can't get the toolbar to switch back to LTR. How should I do it?
you can use
android:layoutDirection="ltr"
only used in API level 17 and higher
Related
I need to change the layout direction inside my app, from LTR to RTL and vise versa, I looked there is no clear solution online, is this process simple or not, I mean it must be property inside the app you can change this property easily for all the layouts in your app, any ideas?
Note: I want to change the language and the layout just inside my app.
Put your device in RTL mode, you can do this in the develop settings (Force RTL layout direction).
You should ensure that all your layouts don't use "left" and "right" properties, e.g layout_marginLeft, instead they should use layout_marginStart and layout_marginEnd.
This will mean that when run on device that's set to read right to left (e.g arabic), all the layouts will mirror correctly.
If you don't want to change the whole device into RTL, paste this in the onCreate method of every activity in your app (paste it before super and before you inflate the content view.
Java: getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
Kotlin: window.decorView.layoutDirection = View.LAYOUT_DIRECTION_RTL
(I don't believe you'll need to add this to every fragment, only activities).
You can force RTL like this
window.decorView.layoutDirection = View.LAYOUT_DIRECTION_RTL
You can also set it to LAYOUT_DIRECTION_LTR for left-to-right layout direction
I have developed an app in English language. When i change my android language to a right-to-left (later rtl) language , my app goes rtl layout but I don't want that. I want my app layout to stay left-to-right (later ltr) whether android language is ltr or rtl.
I know there is layoutdirection in a activity attributes but it's not for APIs below 17.
I'd still say to use this:
Add in styles.xml in your Base App theme style:
<item name="android:layoutDirection">ltr</item>
17+ is legitimate.
Just do this in manifest
android:supportsRtl="false"
with support rtl your layout in english is still ltr.
rtl is on only for languages how japanese and so on.
the most devices are already API 17+.
On emulator or device you can change main language, so you can use rtl or rtl layout depend on selected language.
Try to put all of your resources (layouts, strings, drawable ... etc) in rtl language folder like
layout-ar and remove default layout folder
also use values-ar, drawable-ar .... etc only
i think this will force android to read from it.
Is there any way to force left to right layouts when the device language is any rtl language?
All XML Screens got broken when the language is changed to Arabic?
You can forcefully disable rtl by changing its property supportRtl="false" in your AndroidManifest.xml file.
I have a custom video player Activity.
I am forced to enable RTL Support in my Application.
But doing so will result in a Right-To-Left aligned ProgressBar. (And that looks ugly)
I want my ProgressBar to stay LTR in my RTL enabled Application.
Is there any solution?
P.S. I am using Android 4.2.2
Okay there is a simple solution for 4.2 and higher:
You can set the android:layoutDirection property in XML, it can have either rtl or ltr values.
The property overrides android:supportsRtl property in "application" segment of your manifest file.
I'm creating an android app for a right-to-left specific language. And I'm using ActionBarSherlock (a compatible android action bar library).
The thing I exactly want is how to change the direction of action bar to RTL even the user not sets the default Locale an RTL language.
If anyone has an idea even for standard android Action Bar it's valuable and may help please share it.
Thanks
From Android API Level 17+ it supports RTL natively. To force your entire layout to be RTL including the ActionBar do the following.
Edit your AndroidManifest.xml and add android:supportsRtl="true" to your <application> tag and then add the following line to the top of your Activities' onCreate() method forceRTLIfSupported(); and then insert the follow function into your Activity.
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void forceRTLIfSupported()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
}
Of course that's only helpful for debugging. Use View.LAYOUT_DIRECTION_LOCALE for production builds so your layout is only changed when the user chosen system location/language aka locale supports RTL.
use this, It will be make layout from rtl but this in parent root of your xml
android:layoutDirection="rtl"
android:textDirection="rtl"
We must believe a powerful rule in programming:
Everything will be possible when you know your tools.
So, from now you can use this library to truly RTLize your actionbars:
https://github.com/semsamot/ActionBarRTLizer
And of course it is compatible with API Level 7+ .
Booger is right. The "Nabz Bazar" app that you mentioned does not align to the right on every version of android. As you can see here ABS doesn't really do anything on Android 4.0 or newer and the screenshots of that app are on a 4.2 android.
I am pretty sure you will not be able to do what you are asking. The ActionBar pattern is very specific, and the libraries supporting it would support the defined pattern only (which is having the icon on the top-left, then laying out title, and action buttons to the right).
Changing the test is as simple as changing the locale and text within your app (as you already alluded to).
Changing the position of the elements within the ActionBar itself will not be supported by the libraries that support the standard ActionBar pattern - as what you describe is not a supported UI pattern.
I think you should leave the AB as-is. It is not necessary to switch the order, as there is no right-left orientation anyway -and changing this will be extremely jarring to your users.
Bottom line, you won't be able to to this with the existing libraries - and you probably shouldn't anyway.
You can use one or all of them if your project is completely RTL language.
set on AndroidManifest.xml
<application
android:supportsRtl="true"
>
set in Xml.file
android:layoutDirection="rtl"
android:textDirection="rtl"
set in java.class
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void forceRTLIfSupported()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
}
Kotlin:
Add forceRTLIfSupported() to the top of the onCreate() method of your Activity and then insert the following function into your Activity:
fun forceRTLIfSupported(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
window.decorView.layoutDirection = View.LAYOUT_DIRECTION_RTL
}
}