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.

3
  • 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. Commented Apr 4, 2018 at 11:38
  • Yes, I agree with you. I just want for some entry to prevent font size. So I am looking for this. Commented Apr 4, 2018 at 11:50
  • Here is about the UpdateConfiguration was deprecated. Commented Apr 5, 2018 at 2:44

4 Answers 4

12

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

this makes app crashing on orientation changes via null reference !
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;
    }

}

3 Comments

Phenomenal, many thanks. Insights into iOS equivalent? Possibly in AppDelegate?
@jtth - Haven't been an issue in any iOS devices in my experience. Do you think there will be font size variations in iOS?
@jtth you may have found it already, but the iOS equivalent is a platform specific property set on the Application called EnableAccessibilityScalingForNamedFontSizes learn.microsoft.com/en-gb/xamarin/xamarin-forms/platform/ios/…
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;
    }
}

1 Comment

Perfect! It's better to check the config instance.
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;
                }
            }
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.