7

If I change the device font then it will also change my app font. I do not want to change my app font according the device font.

So I searched about this, and I found Dimension.

Please let me know how to use dp in Xamarin for Android. Also suggest me any other proper way to resolve this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Uddhao Pachrne
  • 531
  • 1
  • 8
  • 22
  • 1
    "any other proper way to resolve this" -- allow your fonts to change size based on the system font size. The user is saying "I would like larger fonts" (or possibly "I would like smaller fonts"). You, by trying to prevent this, are telling the user that you do not care what the user wants. The user, in turn, may not appreciate your app very much. – CommonsWare Apr 04 '18 at 11:38
  • Yes, I agree with you. I just want for some entry to prevent font size. So I am looking for this. – Uddhao Pachrne Apr 04 '18 at 11:50
  • [Here](https://stackoverflow.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated) is about the `UpdateConfiguration` was deprecated. – Robbit Apr 05 '18 at 02:44

4 Answers4

11

Robbit's answer is right, but Resources.UpdateConfiguration() has been deprecated. So, currently, it should be done like this:

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

        return CreateConfigurationContext(config).Resources;
    }
}
Rafael Pereira
  • 321
  • 4
  • 9
8

In your activity, add this:

public override Resources Resources {
    get
    {
        Resources res = base.Resources;
        Configuration config = new Configuration();
        config.SetToDefaults();
        res.UpdateConfiguration(config, res.DisplayMetrics);
        return res;
    }

}
Robbit
  • 4,300
  • 1
  • 13
  • 29
  • Phenomenal, many thanks. Insights into iOS equivalent? Possibly in `AppDelegate`? – jtth Apr 10 '19 at 21:48
  • @jtth - Haven't been an issue in any iOS devices in my experience. Do you think there will be font size variations in iOS? – Abhijith C R Dec 02 '20 at 17:48
  • @jtth you may have found it already, but the iOS equivalent is a platform specific property set on the Application called `EnableAccessibilityScalingForNamedFontSizes` https://learn.microsoft.com/en-gb/xamarin/xamarin-forms/platform/ios/named-font-size-scaling – greg84 Jul 30 '22 at 21:45
8

I used Rafael's answer, but I was getting this error when I tried to set Application.Current.MainPage:

Binary XML file line #1: Error inflating class <unknown>

I solved it doing this:

public override Android.Content.Res.Resources Resources
{
    get
    {
        var config = base.Resources.Configuration;
        if (config == null)
            config = new Configuration();
        config.FontScale = 1f;
        return CreateConfigurationContext(config).Resources;
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marlon Adarme
  • 357
  • 4
  • 15
3

In the Xamarin.Forms Android MainActivity.cs, override the Resources and set the configuration to default to restrict the font size effect on application.

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public override Resources Resources
        {
            get
            {
                Resources resource = base.Resources;
                Configuration configuration = new Configuration();
                configuration.SetToDefaults();
                if (Build.VERSION.SdkInt >= Build.VERSION_CODES.NMr1)
                {
                    return CreateConfigurationContext(configuration).Resources;
                }
                else
                {
                    resource.UpdateConfiguration(configuration, resource.DisplayMetrics);
                    return resource;
                }
            }
        }
}
Manoj Alwis
  • 1,337
  • 11
  • 24