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

- 1,590
- 2
- 16
- 37
-
I've not fount a good solution but I've not tested the solution of 'semsamot' about 'RTLizer' yet. If you tested it tell me about that. Thanks. – Aliaaa Oct 08 '14 at 14:00
7 Answers
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.
-
1thank you for your answer. however I need an answer for compatibility support for lower sdk versions if an answer exists! – Aliaaa Jul 17 '13 at 19:43
-
2unfortunately it's RTL all layout. I just wanna change Action bar direction not all application. what i can do? – Mahdi Jul 01 '15 at 11:35
-
Is that possible to add that snippet code to app class wich extends Application class? (To write it once, not all activities) – Dr.jacky Oct 28 '16 at 13:00
-
1
use this, It will be make layout from rtl but this in parent root of your xml
android:layoutDirection="rtl"
android:textDirection="rtl"

- 1,657
- 1
- 18
- 22
-
2That's fine and all well, however it does not support system made view's like the "native" ActionBar which is created before you inflate your own layout. This code snippet above ONLY affects the layout you are inflating. – Simon Aug 13 '18 at 10:21
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+ .

- 805
- 9
- 11
-
*Cannot resolve symbol 'ActionBarRtlizer'* (I think you forgot to mention what should we import in order to fix that problem) – Ghasem Nov 23 '16 at 08:17
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.

- 914
- 1
- 6
- 14
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.

- 18,579
- 7
- 55
- 72
-
But others made it pretty well take a look on this: https://play.google.com/store/apps/details?id=com.hamrahyar.nabzebazaar – Aliaaa Jul 07 '13 at 14:02
-
My guess is that they used a custom layout to create this element themselves. I think the native (or ActionBarSherlock) implementation doesn't support this at this time (will see if someone has a different answer). – Booger Jul 07 '13 at 17:13
-
-
It looks like native support was added for this functionality in OS 4.2+: https://plus.google.com/+AndroidDevelopers/posts/HuHNSb8V7s8 – Booger Oct 20 '14 at 13:17
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);
}
}

- 668
- 8
- 9
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
}
}

- 43
- 1
- 8