Prevent display size change settings affect app design – Xamarin.forms

Issue

I have xamarin.forms app. When user change the device font size in accessibility settings of android, My apps design gets scrampled. Which I am able to prevent.Now the problem I am facing is when user change the display size in the settings of android again my design gets distorted. How can I prevent this? Any help is appreciated.

Solution

I have xamarin.forms app. When user change the device font size in accessibility settings of android, My apps design gets scrampled. Which I am able to prevent.Now the problem I am facing is when user change the display size in the settings of android again my design gets distorted. How can I prevent this? Any help is appreciated.

You can try the following code in Mainactivity.cs:

   private void initFontScale()
    {
        Configuration configuration  Resources.Configuration;
        configuration.FontScale  (float)1;
        //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.Resources.UpdateConfiguration(configuration, metrics);
        BaseContext.ApplicationContext.CreateConfigurationContext(configuration); 
        BaseContext.Resources.DisplayMetrics.SetTo(metrics);
    }

protected override void OnCreate(Bundle savedInstanceState)
    {
        initFontScale();
        TabLayoutResource  Resource.Layout.Tabbar;
        ToolbarResource  Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);
        ...
        LoadApplication(new App());
    }

you can also try to override attachBaseContext method:

        protected override void AttachBaseContext(Context @base)
    {
        Configuration overrideConfiguration  new Configuration();
        overrideConfiguration  @base.Resources.Configuration;
        overrideConfiguration.SetToDefaults();
        var fontScale  overrideConfiguration.FontScale;
        overrideConfiguration.FontScale  (float)1;
        Context context  @base.CreateConfigurationContext(overrideConfiguration);
        base.AttachBaseContext(context);
    }

Answered By – Cherry Bu – MSFT

Leave a Comment