Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. /*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.
  2.  
  3. Don't forget to add manifest tag android:configChanges="fontScale" since you are hadnling this configuration change yourself.*/
  4.  
  5. @Override
  6. public void onConfigurationChanged(Configuration newConfig) {
  7. super.onConfigurationChanged(newConfig);
  8.  
  9. // In some cases modifying newConfig leads to unexpected behavior,
  10. // so it's better to edit new instance.
  11. Configuration configuration = new Configuration(newConfig);
  12. SystemUtils.adjustFontScale(getApplicationContext(), configuration);
  13. }
  14.  
  15. //In some helper class we have adjustFontScale() method.
  16.  
  17. public static void adjustFontScale(Context context, Configuration configuration) {
  18. if (configuration.fontScale != 1) {
  19. configuration.fontScale = 1;
  20. DisplayMetrics metrics = context.getResources().getDisplayMetrics();
  21. WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  22. wm.getDefaultDisplay().getMetrics(metrics);
  23. metrics.scaledDensity = configuration.fontScale * metrics.density;
  24. context.getResources().updateConfiguration(configuration, metrics);
  25. }
  26. }
  27. //WARNING! That will totally ignore Accessibility Font Scale user settings and will prevent your App fonts scaling!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement