2

I want to enable RTL support in my android application. I have checked this question change action bar direction to right-to-left

I have done the below things:

  1. Added android:supportsRtl="true" to the <application> element in manifest file
  2. And Added the below function in onCreate method of MainActivity.java class.

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

But it changes the MainActivity's layout only from LTR to RTL. Now I want to change the layout of each activity of my app, how can I do that. Please help me in anyone has any idea about RTL support in all over the app.

Community
  • 1
  • 1
Prithniraj Nicyone
  • 5,021
  • 13
  • 52
  • 78

1 Answers1

2

You can create a base activity and extend all your activities from it:

BaseActivity.java

import android.annotation.TargetApi;
import android.os.Build;

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        forceRTLIfSupported();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void forceRTLIfSupported()
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }
    }

}

SampleActivity.java

public class SampleActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);
    }

}
onooma
  • 46
  • 4