4

I am working on LGE Nexus 5x which runs on Android 7.0 & I want to restrict any system font changes to change my application font size.

I have already gone through this question, and also I have tried this answer which works perfectly upto Marshmallow. But doesn't work on Nougat.

In my Application class I have added these lines;

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    adjustFontScale(new Configuration(newConfig));
}


public void adjustFontScale(Configuration configuration) {
    if (configuration != null && configuration.fontScale != 1.0) {
        configuration.fontScale = (float) 1.0;
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        metrics.scaledDensity = configuration.fontScale * metrics.density;
        getBaseContext().getResources().updateConfiguration(configuration, metrics);
    }
}

In manifest, under application tag I have declared;

android:configChanges="fontScale"

But that doesn't work on Nougat devices even though onConfigurationChanged is called and executed without any exception.

Any help will be appreciated.

Community
  • 1
  • 1
Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56

1 Answers1

2

I had the same problem. I managed to solve it by overriding onConfigurationChanged in Activity instead of Application class & changing getBaseContext() to

this.getResources().updateConfiguration(configuration, metrics);
Nayan
  • 367
  • 4
  • 18