Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources.UpdateConfiguration is obsolete

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;
    }

}
like image 648
D.madushanka Avatar asked Oct 27 '25 03:10

D.madushanka


1 Answers

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));
}
like image 85
Lucas Zhang Avatar answered Oct 29 '25 19:10

Lucas Zhang



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!