7

I want to allow user select font size.

Spare Parts do provide the feature to do that,

ActivityManagerNative.getDefault().updateConfiguration(new font size scale config)

but that is for whole all android applications. I want to add it in my application's preference and only impact for my app.

I tried below methods, it do works sometimes, but the first time I start my application, the font size still with the 1.0 scale. I do not why. I added below code in every onCreate of my activities.

Configuration config = resources.getConfiguration(); config.fontScale=1.5f; resources.updateConfiguration(config, mDisplayMetrics);

What can I do?

virsir
  • 15,159
  • 25
  • 75
  • 109
  • did you find an answer for this? I am having the same problem at the moment... – dineth Nov 28 '11 at 02:20
  • see Activity.getResources().setConfiguration() (or something like that) – njzk2 Jan 05 '12 at 15:21
  • https://stackoverflow.com/questions/21546805/how-to-prevent-system-font-size-changing-effects-to-android-application/53970324#53970324 - that's how you update config and get font scale overriden for your app – Maxim Saplin Dec 29 '18 at 14:20

3 Answers3

18

The code below will allow to change for the application only the font scale. On second line, the code "configuration.fontScale=(float) 1;" will have to chabge to meet your needs. The defaults are scaling with 0.15 step, having default 1.

Place it before calling setContentView(R.layout.yourlayout)

Configuration configuration = getResources().getConfiguration();
configuration.fontScale=(float) 1; //0.85 small size, 1 normal size, 1,15 big etc

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.scaledDensity = configuration.fontScale * metrics.density;
getBaseContext().getResources().updateConfiguration(configuration, metrics);

Hope this helps!

arionargo
  • 177
  • 1
  • 5
  • 1
    Don't use this in your Application class's OnConfigurationChanged; some of the devices I have seem to freak out and blink uncontrollably when rotating. Putting it after your super.onCreate seems to work out well and OnConfigurationChanged will call your onCreate again anyways, so you're set if a user decides to change the font in the middle of your application. – welshk91 Sep 28 '15 at 21:16
  • 1
    not working for android 10 and some android 9... but works for android with lower versions – Azin Nilchi Aug 04 '20 at 17:24
1

Translated for Xamarin....

        Android.Content.Res.Configuration configuration = Resources.Configuration;
        configuration.FontScale = (float)1; //0.85 small size, 1 normal size, 1,15 big etc

        Android.Util.DisplayMetrics metrics = new DisplayMetrics();
        this.WindowManager.DefaultDisplay.GetMetrics(metrics);
        metrics.ScaledDensity = configuration.FontScale * metrics.Density;
self.name
  • 2,341
  • 2
  • 17
  • 18
0

I think what you are looking for is to apply a theme to your activity or view

Please take a look at the android docs Themes

Idistic
  • 6,281
  • 2
  • 28
  • 38