use locale (ltr/rtl) for gravity in TextView

I have a simple TextView which should have android:gravity="left" for ltr system locales and android:gravity="right" for rtl system locales.

The obvious choice would be: android:gravity="start" but then e.g. english text will always be left-aligned and hebrew right-aligned.

  • android:how to check if application is running in background
  • How to use jarsigner for signing an apk?
  • <compatible-screens> excludes app from Nexus 5x in Google Play
  • How to handle error states with LiveData?
  • Android - java.lang.IllegalArgumentException: contentIntent required error caused by notification?
  • ADK and Eclipse woes - and USBAccessory?
  • Here is how it looks with android:gravity="start":

    LTR locale:

    |          לורם|   // incorrect
    |test           |  // correct

    RTL locale:

    |          לורם|  // correct
    |test           |  // incorrect

    it’s supposed to look like that:

    LTR locale:

    |לורם           |  
    |test            | 

    RTL locale:

    |          לורם|  
    |           test| 

    Is it possible to do that without using a layout-ldrtl folder with a modified xml file? This would complicate development a lot because I would have to edit a lot of layout files twice…

    edit: a solution for API 17+ is enough.
    I wrote system locale, but actually I’m allowing the user to change the app language like that:

    Configuration configuration = context.getResources().getConfiguration();
    configuration.locale = selectedLocale;
    context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());

    so it would be great if this locale would be considered for the rtl <-> ltr choice.

    Related posts:

    Is there a way to define a min and max value for EditText in Android?
    How can I change language of my application
    Saving and restoring view state android
    How to Copy Image File from Gallery to another folder programmatically in Android
    Android V7 Support Library Popup Menu
    Google Map Android Api V2 Sample Code not working
  • Video clarity with my code is bit low as compared to recording with Android camera
  • Android open emailclient programmatically
  • Android 6.0 Marshmallow : Weird error with fragment animation
  • How to determine a long touch on android?
  • Android + NoSQL
  • ListView scrollbarStyle with margin/padding
  • 4 Solutions collect form web for “use locale (ltr/rtl) for gravity in TextView”

    I had this problem too, you can use these properties with the TextView


    or just add it to the Base App Theme in styles.xml
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="colorButtonNormal">@color/colorAccent</item>
    <item name="android:textAlignment">gravity</item>
    <item name="android:textDirection">locale</item>

    that worked for me.


    If text view is stretched to parent width, there is no need to change the gravity of the element. Just align the text to the view start, the text will move to LTR or RTL position, independent of the text content (hebrew or western).

    You probably need a special extended TextView class for that:

    public class LocaleAwareTextView extends TextView {
        public LocaleAwareTextView(Context context) {
            setGravity(getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL ? Gravity.RIGHT : Gravity.LEFT);
        public LocaleAwareTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setGravity(getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL ? Gravity.RIGHT : Gravity.LEFT);
        public LocaleAwareTextView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            setGravity(getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL ? Gravity.RIGHT : Gravity.LEFT);

    … and use this class in your layouts. If you want to switch the locale and all orientations on the fly, you will need to reload your layouts.

    I’m using this solution now, I would prefer a simple xml solution, but this seems minimal so far:

      if (DynamicLanguage.getLayoutDirection(getContext()) == View.LAYOUT_DIRECTION_RTL) {
      } else {
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.