Home » Android » java – React native RTL on Android

java – React native RTL on Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

Trying to get these few steps here so I can support RTL:
Making an App RTL-ready
I am trying to add these lines to MainActivity.java according to the instructions:

I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
sharedI18nUtilInstance.setAllowRTL(context, true);

enter image description here

(I am not a native Android developer, but trying to use react native and occasionally accessing the Android source to modify deeper changes – this one won’t compile.)

First, where do they think context should be defined? I can’t imagine it’s any sort of a global..?

Second, setAllowRTL shows in red… which seems like a compilation error.
Any idea what they meant? am I even defining this in the right place?

My code looks like this:

import com.facebook.react.ReactActivity;
import com.facebook.react.modules.i18nmanager.I18nUtil;

import android.content.Intent;

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "myApp";
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
        sharedI18nUtilInstance.setAllowRTL(context, true);
        MainApplication.getCallbackManager().onActivityResult(requestCode, resultCode, data);
    }
}
How to&Answers:

Add this import in MainApplication.java :

import android.os.Bundle; // needed for onCreate method
import com.facebook.react.modules.i18nmanager.I18nUtil;

and add

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
    sharedI18nUtilInstance.allowRTL(getApplicationContext(), true);
}

Answer:

Add this import in MainActivity.java :

import com.facebook.react.modules.i18nmanager.I18nUtil;
I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
sharedI18nUtilInstance.allowRTL(getApplicationContext(), true);

add these line in ReactActivityDelegate

  protected ReactActivityDelegate createReactActivityDelegate() {
            return new ReactActivityDelegate(this, getMainComponentName()) {
      @Override
      protected ReactRootView createRootView() {
          I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
          sharedI18nUtilInstance.allowRTL(getApplicationContext(), true);
                       return new RNGestureHandlerEnabledRootView(MainActivity.this);
                      }
    };
          }