Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change action bar direction to right-to-left

Tags:

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

like image 561
Aliaaa Avatar asked Jul 07 '13 13:07

Aliaaa


People also ask

How do I customize my action bar?

This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 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.

Hope that helps.

like image 119
Simon Avatar answered Oct 12 '22 14:10

Simon


use this, It will be make layout from rtl but this in parent root of your xml

    android:layoutDirection="rtl"
    android:textDirection="rtl"
like image 30
Amal Kronz Avatar answered Oct 12 '22 13:10

Amal Kronz