Force RTL on LTR devices

Viewed 6857

The title says it all. I found a couple of ways to set the application's default layout to RTL using Facebook's I18nManager, but it does that only on second app launch.

code example: I18nManager.forceRTL(true)

I do not want to give users the ability to change languages because the application itself is in Arabic. I've searched everywhere but all talk about how to support RTL and not actually use it as the default layout.

Is there a way to achieve this with I18nManager or do I have to do a couple of changes to my native code?

4 Answers

MageNative's answer is correct. The only thing is setting the locale like that is deprecated. You need to use the setLocale() method, like this:

Locale locale = new Locale("fa_IR"); // This is for Persian (Iran)
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
getApplicationContext().getResources().updateConfiguration(config, getApplicationContext().getResources().getDisplayMetrics());

You'll need to import java.util.Locale and android.content.res.Configuration.

You have to do this in two places:

First: In your Manifest's application tag, add support for RTL like this:

<application
    ...
    android:supportsRtl="true"
    ..>
</application>

Second one: Add this as first line of your onCreate() method of your activity:

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

If you have a base activity, you can do the same and it will be applied to other child activities.

Related