Force RTL on LTR devices

The title says it all. I found several ways to set the default layout for an RTL app using Facebook's I18nManager, but it only does this when the second app starts.

example code: I18nManager.forceRTL(true)

I don't want to give users the option to change languages ​​because the app itself is in Arabic. I've searched everywhere but everyone is talking about how to maintain RTL rather than using it as the default layout.

Is there a way to achieve this using I18nManager, or do I need to make a couple of changes to my native code?

+5


source to share


3 answers


First add RTL support in your manifest like so:

<application
    android:name=".application.SampleApplication"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
</application>

      



Then in your activity onCreate()

add the following code to onCreate()

.

Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale; 
getApplicationContext().getResources().updateConfiguration(config, getApplicationContext().getResources().getDisplayMetrics());

      

+8


source


add this line of code to the very top of the onCreate method (before super and setContentView) of all your actions:

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

      



and make sure you have Rtl support set to True in your manifest.

+2


source


MageNative's answer is correct. The only thing is setup locale

like this is outdated. You need to use a method setLocale()

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 will need to import java.util.Locale

and android.content.res.Configuration

.

0


source







All Articles