Tallboy
Tallboy

Reputation: 13407

How to disable font scaling in RN for Android?

Simply using allowFontScaling={false} fixes this on iOS, but I'm unsure how to set this on Android.

Also, for anyone that's not familiar with RN and is coming here from the Android tag, I don't think I can easily change from to dp font scaling or whatever, so is there a way I can do it globally in my app somehow?

Thanks!

Upvotes: 5

Views: 6563

Answers (1)

Nisarg Thakkar
Nisarg Thakkar

Reputation: 1547

Please Update ManiApplication.java file

import android.view.WindowManager;
import android.content.res.Configuration;
import android.content.Context;
import android.util.DisplayMetrics;

After super.onCreate(); method put adjustFontScale(getApplicationContext(),getResources().getConfiguration());

Like follow

  @Override
  public void onCreate() {
    super.onCreate();
    adjustFontScale(getApplicationContext(),getResources().getConfiguration());
}

And at the end add the following function

public void adjustFontScale(Context context, Configuration configuration) {
    if (configuration.fontScale != 1) {
        configuration.fontScale = 1;
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        metrics.scaledDensity = configuration.fontScale * metrics.density;
        context.getResources().updateConfiguration(configuration, metrics);
    }
}

Upvotes: 13

Related Questions