How can I prevent the device font size effect of a MAUI Android app?

Bhuwan 886 Reputation points
2024-03-08T11:43:30.2466667+00:00

How can I prevent the device font size effect of a MAUI Android app? when i change font or size in android then it's reflect on MAUI application i want to restrict this.

Developer technologies | .NET | .NET MAUI
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-03-12T07:24:43.3+00:00

    Hello,

    You could override the Resource in the MainActivity to cover the font size of the system.

    You could add the following code into Platforms/Android/MainActivity.cs to implement this feature.

    
    // add the overrided property into MainActivity class
          public override Resources? Resources {
                get
                {
                    Resources resource = base.Resources;
                    Configuration configuration = new Configuration();
                    configuration.SetToDefaults();
                    if (Build.VERSION.SdkInt >= BuildVersionCodes.NMr1)
                    {
                        return CreateConfigurationContext(configuration).Resources;
                    }
                    else
                    {
                        resource.UpdateConfiguration(configuration, resource.DisplayMetrics);
                        return resource;
                    }
                }
            }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Azar 29,520 Reputation points MVP Volunteer Moderator
    2024-03-08T11:47:43.61+00:00

    Hey there Bhuwan

    Thats a good question and thanks for using QandA platform

    So to prevent this you can disable font scaling at the application level. the font size remains consistent regardless of the device settings.

    In your Xamarin Android project, open the MainActivity.cs file

    Inside the OnCreate() method, add the code I put below

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Disable font scaling
        Resources.Configuration.FontScale = 1.0f;
        // Your existing code goes here
    }
    
    
    

    The FontScale property of the device's Resources.Configuration to 1.0 disables rhe font scaling.and the font size in your Xamarin Android app will remain consistent regardless of the device font size settings.

    If this helps kindly accept the answer thanks much.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.