3

I managed to support rtl layouts when I have turned my phone's language in aramaic and have set my app's locale accordingly. What I haven't managed to do, and to be honest I wonder if it's possible, is to support rtl layouts when app's locale is set in a language as so (e.g. new Locale("ar")) and my phone's language is in English. When I do so, my app follows an ltr direction and to generalize it I noticed that the text direction of an app is completely depended to phone's language, ignoring locale as for the text direction. So does anybody know how to support rtl logic in an app with a suitable locale but the phone is turned in English? Or if is this even possible?

Edit

Thanks to all the guys that answered the question but their replies don't answer my question. I probably have not made it clear but I managed to support rtl when I have turned my phone in an right-to-left language and forced rtl in developer options. My question is is it possible to have a "right-to-left app" when phone's language is, for example, English but my app's locale is, for example, Aramaic? Or everything will be shown in a "left-to-right" logic but with the respective aramaic translations?

Paebbels
  • 15,573
  • 13
  • 70
  • 139
antonis_st
  • 468
  • 3
  • 16
  • To clarify... Briefly you want to have rtl compatibility with phones with English language set? Like in app change locale to some that has rtl? – Alberto Jun 16 '17 at 09:47
  • Yeah. When the user changes to an rtl locale I want the layout to be set in a respective way (toolbar aligned to right, texts gravity right etc etc.). – antonis_st Jun 16 '17 at 09:57

4 Answers4

1

To fully support rtl do the following:

Step 1:

Add the following to the manifest file.

<application
    android:supportsRtl="true">

    ...

</application>

Step 2:

Translate all strings at your translator editor. help here.

Step 3:

Update your layouts.

Instead of paddingLeft / paddingRight use paddingStart and paddingEnd, the app will determine on which language it runs and will put the padding or margin accordingly. And do the same for the Margins.

Step 4:

Use the following method to check at runtimes if the app runs in Right to left mode.

public boolean isRTL(Context ctx) {
  Configuration config = ctx.getResources().getConfiguration();
  return config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
}

So... If you just want to change apps locale use the following code:

Locale locale = new Locale("ru");
Locale.setDefault(locale);
Configuration config = 
  getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
  getBaseContext().getResources().getDisplayMetrics());
Alberto
  • 1,423
  • 18
  • 32
0

add this line of code to the most top of the onCreate method (before super and setContentView) of every activity you want to be rtl. It will force the layout to be RTL

 getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
Gil Goldzweig
  • 1,809
  • 1
  • 13
  • 26
0

Follow below link for support right to left for Arabic and left to right for other language.

https://android-developers.googleblog.com/2013/03/native-rtl-support-in-android-42.html

MayurDev
  • 189
  • 2
  • 8
0

What did the trick for me was to use conf.setLayoutDirection(currentLocale); when updating my resources configuration. So to answer my own question...Yes, it's possible to have an rtl application while the phone's is turned to an ltr language.

antonis_st
  • 468
  • 3
  • 16