3

EDIT 2

sample

EDIT

Override attachBaseContext method to update the context in activity

protected override void AttachBaseContext(Context @base)
{
    base.AttachBaseContext(@base);
}

end edit

In Android API 25 Resources.UpdateConfiguration(Configuration, DisplayMetrics) was deprecated and it's advised to use Context context = CreateConfigurationContext(Configuration); instead.

Current implementation

public override Resources Resources
{
    get
    {
        Resources res = base.Resources;
        Configuration config = new Configuration();
        config.SetToDefaults();

        res.UpdateConfiguration(config, res.DisplayMetrics);
        return res;
    }
}

Referencing Android context.getResources.updateConfiguration() deprecated as a guide, tried the following:

public override Resources Resources
{
    get
    {
        Configuration overrideConfiguration = base.Resources.Configuration;
        overrideConfiguration.SetToDefaults();
        Context context = CreateConfigurationContext(overrideConfiguration);
        Resources res = context.Resources;
        return res;
    }
}

However this produces anomalous errors..

Android.Views.InflateException: Error inflating class
com.android.internal.widget.DialogTitle

How to correctly implement Context context = CreateConfigurationContext(Configuration)?

Note 'Current implementation' works perfectly fine but as encouraged to not use deprecated code want to the replacement working

jtth
  • 876
  • 1
  • 12
  • 40
  • have you override attachBaseContext method to update the context in activity ? – Leo Zhu Apr 11 '19 at 08:45
  • `base.AttachBaseContext(@base);` here should use your new context not @base – Leo Zhu Apr 19 '19 at 02:18
  • try to add `Configuration overrideConfiguration = @base.Resources.Configuration; overrideConfiguration.SetToDefaults(); Context context = CreateConfigurationContext(overrideConfiguration); base.AttachBaseContext(context);` to attachBaseContext method,then in your Resources override call `Resources res = context.Resources;return res;` – Leo Zhu Apr 24 '19 at 03:15
  • @LeoZhu-MSFT Visual added. `@base` returns null?`context.Resources` how are we passing context here, should this be `base.Resources`? – jtth Apr 24 '19 at 17:07
  • yes,base.Resources,not sure of success, but give it a try – Leo Zhu Apr 25 '19 at 01:17
  • could you share your project ?That way maybe could find the mistakes more accurately! – Leo Zhu Apr 25 '19 at 09:41

1 Answers1

3

Not sure if it's your need,you could change the attachBaseContext method like this,it could work:

protected override void AttachBaseContext(Context @base)
    {
        Configuration overrideConfiguration = new Configuration();
        overrideConfiguration = @base.Resources.Configuration;
        overrideConfiguration.SetToDefaults();
        Context context = @base.CreateConfigurationContext(overrideConfiguration);
        base.AttachBaseContext(context);
    }
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23