1

How to disable system font size scaling for maui app? android 33 version.

I tried to set configuration to default within MainActivity:

public override Android.Content.Res.Resources Resources
{
    get
    {
        Android.Content.Res.Configuration configuration = new Android.Content.Res.Configuration();
        configuration.SetToDefaults();

        DisplayMetrics metrics = new DisplayMetrics();

        if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.NMr1)
        {
            return CreateConfigurationContext(configuration).Resources;
        }
        else if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
        {
            base.Resources.UpdateConfiguration(configuration, metrics);
            return base.Resources;
        }
        else
        {
            return base.Resources;
        }
    }
}

in debug it resets display metrics density to default, but once in a while i see scaled font as system one. Also try tried to set:

configuration.FontScale = (float)1;

but the same.

  • Try adapting one of the Xamarin answers. https://stackoverflow.com/questions/49649874/how-can-i-prevent-the-device-font-size-effect-of-a-xamarin-android-app, https://stackoverflow.com/questions/74363019/preventing-android-12-system-font-size-from-affecting-in-app-font-size-crash, https://stackoverflow.com/questions/50289763/disable-system-font-size-effect-in-my-app. – ToolmakerSteve May 09 '23 at 01:12

1 Answers1

1

You can put the code below in the MainActivity.cs file. It can prevent to change the application font to match the system font.

 protected override void AttachBaseContext(Context @base)
     {
         Configuration configuration = new(@base.Resources.Configuration)
         {
             FontScale = 1.0f
         };
         ApplyOverrideConfiguration(configuration);
         base.AttachBaseContext(@base);
     }
Guangyu Bai - MSFT
  • 2,555
  • 1
  • 2
  • 8