How to replace deprecated Android “Context.Resources.UpdateConfiguration” in Xamarin.Form project?

solidcomersolidcomer Member
edited July 27 in Xamarin.Android

I'm using this Android API Context.Resources.UpdateConfiguration(Configuration, DisplayMetrics), but it's deprecated, so it doesn't work in higher version of Android system. I've done research online but found only Java solutions, I'm not able to translate that into C# in my Xamarin.Form project.

Google doc says it's updated to Context.CreateConfigurationContext(Configuration), but this doesn't work for me (the DisplayMetrics is not even evolved).

In Android part, there is this function:

public void SetStandardFontSize()
    {
        Configuration configuration = Android.App.Application.Context.Resources.Configuration;
        configuration.FontScale = (float)1;
        DisplayMetrics metrics = new DisplayMetrics();
        (MainActivity.mainContext as MainActivity).WindowManager.DefaultDisplay.GetMetrics(metrics);
        metrics.ScaledDensity = configuration.FontScale * metrics.Density;

    //deprecated
        Android.App.Application.Context.Resources.UpdateConfiguration(configuration, metrics);      
    }

I just want to use the above function (learned from stackoverflow post) to overwrite Android Accessibility Font Scale. The above function only works up to Android 6. So I'm looking for a correct way of updating Configuration and DisplayMetrics, which works in all versions of Android.

I tried to replaced the API with the followings,

Android.App.Application.Context.Resources.DisplayMetrics.SetTo(metrics); 
Android.App.Application.Context.Resources.Configuration.SetTo(configuration);

But it works only up to Android 6 (maybe 7 too), same as the deprecated Context.Resources.UpdateConfiguration(Configuration, DisplayMetrics).

Thank you very much.

PS: It looks like the updated API comes in Build.VERSION.SDK_INT >= Build.VERSION_CODES.N

Sign In or Register to comment.