antonis_st
antonis_st

Reputation: 468

Is it possible to support rtl text direction according to app's locale while phone is in English?

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?

Upvotes: 3

Views: 758

Answers (4)

antonis_st
antonis_st

Reputation: 468

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.

Upvotes: 0

MayurDev
MayurDev

Reputation: 199

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

Upvotes: 0

Alberto
Alberto

Reputation: 1489

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());

Upvotes: 1

Gil G
Gil G

Reputation: 1869

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);

Upvotes: 0

Related Questions