4

I want to prevent system font-size changing, effects to my xamarin forms android application. I tried with below code in MainActivity.cs and its working, but it tells me that it is deprecated (res.UpdateConfiguration). I tried some other codes, but no luck.

public override Resources Resources
{
    get
    {
        Resources res = base.Resources;
        Configuration config = new Configuration();
        config.SetToDefaults();
        res.UpdateConfiguration(config, res.DisplayMetrics);
        return res;
    }

}
D.madushanka
  • 429
  • 6
  • 17

2 Answers2

6

context.getResources().updateConfiguration() has been deprecated in Android API 25 and it is advised to use context.createConfigurationContext() instead.

public override Resources Resources
{
  get
  {              
   Configuration config = new Configuration();
   config.SetToDefaults();
               
   Context context = CreateConfigurationContext(config);
   Resources resources = context.Resources;

   return resources;
  }
}

Check Android context.getResources.updateConfiguration() deprecated

Update

If you want to change the font size, you should override the method AttachBaseContext.

Java

protected override void AttachBaseContext(Context @base)
{
  // base.AttachBaseContext(@base);
  Configuration config = new Configuration();
  config.SetToDefaults();
  config.FontScale = 1.0f;
  Context context = @base.CreateConfigurationContext(config);
  base.AttachBaseContext(context);
}

Kotlin

override fun attachBaseContext(newBase: Context) {
    
    val config =  Configuration();
    config.setToDefaults();
    config.setLocale(Locale("mr"))
    
    super.attachBaseContext(newBase.createConfigurationContext(config));
}
Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22
  • i put this code inside the MainActivity.cs but no change to the interface after changing the system font size – D.madushanka Jul 19 '19 at 09:41
  • still not working. I am testing on my xiaomi mi4c android 7 phone. I think **DisplayMetrics** is do the different. in deprecated one it has the **DisplayMetrics** – D.madushanka Jul 19 '19 at 10:56
  • its working. i did a mistake. I have checked in a API 24 phone both times. That was the problem. thank you – D.madushanka Jul 22 '19 at 05:40
  • I get `Configuration is a namespace but is used like a type` error. – Oiproks May 19 '20 at 09:16
  • For java most of the Methods are with small letters in case you wonder the errors after copy pasting: - attachBaseContext - setToDefaults - config.fontScale - base.createConfigurationContext – FrankKrumnow Nov 23 '20 at 10:52
1

If you don't like using protected override, you can use

private void initFontScale()
{
     Configuration configuration = Resources.Configuration;
     configuration.FontScale = (float)1.45;
     //0.85 small, 1 standard, 1.15 big,1.3 more bigger ,1.45 supper big 
     DisplayMetrics metrics = new DisplayMetrics();
     WindowManager.DefaultDisplay.GetMetrics(metrics);
     metrics.ScaledDensity = configuration.FontScale * metrics.Density;
     BaseContext.ApplicationContext.CreateConfigurationContext(configuration); BaseContext.Resources.DisplayMetrics.SetTo(metrics);
}

protected override void OnCreate(Bundle bundle)
{
    initFontScale();
    ...
}
Minh Seven
  • 21
  • 2