Anand
Anand

Reputation: 1959

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

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.

Upvotes: 2

Views: 1666

Answers (2)

Aaron Ramos
Aaron Ramos

Reputation: 97

I want to share my solution for the same problem, hopefully it can be helpful for someone.

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);
  
  //I added the next lines to keep the density.
  
  metrics.Density = configuration.FontScale * (DisplayMetrics.DensityDeviceStable / 160 f);
  metrics.ScaledDensity = metrics.Density;
  configuration.DensityDpi = (int) DisplayMetrics.DensityDeviceStable;
  metrics.DensityDpi = (DisplayMetricsDensity) DisplayMetrics.DensityDeviceStable;
  
  BaseContext.ApplicationContext.CreateConfigurationContext(configuration);
  BaseContext.Resources.DisplayMetrics.SetTo(metrics);

}

Upvotes: 0

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10346

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

Upvotes: 2

Related Questions