I am currently facing a problem - for the third time -.-. We are developing an app with xamarin. So far so good. But on Android there is a little issue. Since Samsung decided it had to sell a "Fold" smartphone, our client came up with a little problem. I don't know if this only occurs because he is not the youngest anymore or its because of the smartphone. In the end it doesn't matter..
To stop beating around.. Our Android app changes the font size if the system font size gets changed. This leads to UI bugs in the whole app. Of course we could apply our complete app to be more responsive. But to be honest, our target group are between 15 and 25. So in 99.9% of the time, the user has not changed the system font size.
I already figured out/found a solution in the internet.
So I added this to my MainActivity.cs in my Android-project.
public override Android.Content.Res.Resources Resources
{
get
{
//Prevent system font size from affecting in -app font size
Android.Content.Res.Configuration config = base.Resources.Configuration;
if (config == null)
config = new Android.Content.Res.Configuration();
config.FontScale = 1f;
return CreateConfigurationContext(config).Resources;
}
}
Til today it worked fine but then i updated my simulator to Android 12 and I am facing a new problem with this code. When I want to play a video or open a image in our app, the app crashes with a NullPointerException. If I delete this code, the system font size won't be ignored anymore.
Has anyone faced a similar problem and maybe already found a solution?
So I added this to my MainActivity.cs in my Android-project.
public override Android.Content.Res.Resources Resources
{
get
{
//Prevent system font size from affecting in -app font size
Android.Content.Res.Configuration config = base.Resources.Configuration;
if (config == null)
config = new Android.Content.Res.Configuration();
config.FontScale = 1f;
return CreateConfigurationContext(config).Resources;
}
}
Up to Android 11 it worked just fine. But since Android 12 I can't play videos or open images in our app.
CodePudding user response:
You can try to put the following code into the MainActivity to prevent android system font size from affecting in-app font size.
protected override void AttachBaseContext(Context @base)
{
Configuration configuration = new Configuration(@base.Resources.Configuration);
configuration.FontScale = 1.0f;
ApplyOverrideConfiguration(configuration);
base.AttachBaseContext(@base);
}