0

Is there a way to set/override the supports-RTL property in Manifest at runtime in Android? I wish to be able to switch the layout programmatically without actually changing the locale. From what I have searched, it looks like I'll have to set layout direction for Application and every activity. Is there a simpler solution?

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

Update:

From this answer, it is possible to change layout direction in all activities at once using a language with RTL layout direction. The answer uses Farsi language for RTL. You can use English if you want LTR.

Place the following code in the method onCreate of your launcher activity before calling setContentView:

Configuration configuration = getResources().getConfiguration();
configuration.setLayoutDirection(new Locale("fa")); // Farsi for RTL
getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());

Old answer:

You can get the reference of decor view in each activity and set its layout direction.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}

To make this process shorter, put the above code inside onCreate method of a common class BaseActivity which extends AppCompatActivity and use all activities as its subclasses.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59