Forum Xamarin.Android

Set font for entire app

NielsCupNielsCup NLMember ✭✭✭

Hi, I am trying to change the font for my entire app, using reflection. I have found this post on SO but it is in java and I have some problems converting it to C#:

java code:

final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
staticField.set(null, newTypeface);

I have tried the following C# code:

var staticTypefaceFieldName = "Monospace";
Typeface newTypeFace = Typeface.CreateFromAsset(context.Assets, @"fonts/mycustomfont.ttf");

var font = typeof(Typeface).GetProperty(staticTypefaceFieldName);    
font.SetValue(null, newTypeface);

But this throws the following error:

Set Method not found for 'Monospace'

Does anybody know how to properly convert the above java code to C#?


  • NielsCupNielsCup NLMember ✭✭✭
    edited August 2017

    OK, it should be something like this:

    var staticTypefaceFieldName = "MONOSPACE";    
    var typeFace = Class.FromType (typeof (Typeface));
    var customfont = Typeface.CreateFromAsset(context.Assets, @"fonts/mycustomfont.ttf");
    var font = typeFace.GetDeclaredField (staticTypefaceFieldName);
    font.Accessible = true;
    font.Set (null, customFont);
Sign In or Register to comment.