Skip to content

Instantly share code, notes, and snippets.

@hitesh-dhamshaniya
Created September 4, 2019 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitesh-dhamshaniya/ba939fae5ebd22193a0f1125a54eea9a to your computer and use it in GitHub Desktop.
Save hitesh-dhamshaniya/ba939fae5ebd22193a0f1125a54eea9a to your computer and use it in GitHub Desktop.
Handle System Font Size. Regardless of system font size apply application font size
/*That's how we do it. In Application class override onConfigurationChanged() like this. If you want different behavior for different activities - override onConfigurationChanged() in Activity.
Don't forget to add manifest tag android:configChanges="fontScale" since you are hadnling this configuration change yourself.*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// In some cases modifying newConfig leads to unexpected behavior,
// so it's better to edit new instance.
Configuration configuration = new Configuration(newConfig);
SystemUtils.adjustFontScale(getApplicationContext(), configuration);
}
//In some helper class we have adjustFontScale() method.
public static 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);
}
}
//WARNING! That will totally ignore Accessibility Font Scale user settings and will prevent your App fonts scaling!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment