1

I have Android application for which I need to force the entire app's layout to RTL. I'm doing this by the use of the xml attribute android:layoutDirection="rtl".

It's all working fine until in one of my Activities I need to manually finish() the Activity and return back to the previous Activity and henceforth every view that I have manually forced the RTL direction on, reverts back to the LTR mode until I fully close the app and start it again.

I am using this piece of code in my BaseActivity to also force the language which is working fine thus far.

override fun onResume() {
    applyLang()
    super.onResume()
}
private fun applyLang() {
    val dm = resources.displayMetrics
    val conf = resources.configuration
    val locale = Locale(FA_LANG.toLowerCase())
    Locale.setDefault(locale)
    conf.setLocale(locale)
    resources.updateConfiguration(conf, dm)
}

Notes: I have tried putting the following code in the onCreate of my BaseActivity to no avail:

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
2hamed
  • 8,719
  • 13
  • 69
  • 112

2 Answers2

1
private RelativeLayout mRoot;

Initialize your root layout object variable as below :

In Activity:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourLayout);

         mRoot= findViewById(R.id.message_single_layout);
    }

in Fragment:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mRoot= (RelativeLayout) view;
}

And where ever you want set your root layout Direction to rtl (if your minimum SKD is at least 17). rtl means Right to left

mRoot.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

yasser
  • 43
  • 1
  • 1
  • 7
-1

add this to your Styles.xml

        <item name="android:layoutDirection">rtl</item>