Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid changing font scale of app when changed from system settings on Android 7.0 [duplicate]

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.

like image 949
Ichigo Kurosaki Avatar asked Oct 24 '25 05:10

Ichigo Kurosaki


1 Answers

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);
like image 66
Nayan Avatar answered Oct 26 '25 20:10

Nayan



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!