Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the context in react-native

I want to make my react-native applicaiton RTL.

As mention in https://facebook.github.io/react-native/blog/2016/08/19/right-to-left-support-for-react-native-apps.html this could do the job:

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

How ever to get the context I used below:

sharedI18nUtilInstance.allowRTL(getApplicationContext(),true);

But the I get NullPointerException

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
    at com.nativestarterkit.MainActivity.getMainComponentName(MainActivity.java:18)
    at com.facebook.react.ReactActivity.createReactActivityDelegate(ReactActivity.java:48)
    at com.facebook.react.ReactActivity.<init>(ReactActivity.java:32)
    at com.nativestarterkit.MainActivity.<init>(MainActivity.java:8)
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
    ... 9 more
like image 634
Alireza Fattahi Avatar asked Mar 04 '26 10:03

Alireza Fattahi


1 Answers

You can use this code in your MainActivety.java file to force your app to be RTL or LTR. I used it and it worked for me.

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

@Override
public void onCreate() {
    super.onCreate();

    // FORCE LTR
    I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
    sharedI18nUtilInstance.allowRTL(getApplicationContext(), false);
    ....
}
like image 78
Vahid Boreiri Avatar answered Mar 05 '26 22:03

Vahid Boreiri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!