I'm a beginner in Xamarin and xml. I know a some C#. I'm trying to set the font of my Xamarin Forms application on the android version specifically, that way I can get a monospace font (Droid Sans Mono). I've looked around at a lot of threads about fonts and OnPlatform and data bindings, but I'm a little bit lost. I'm pretty sure Font.FontFamily
is read-only as well.
here are some examples of things I have tried:Label1.FontFamily = Device.OnPlatform("MarkerFelt-Thin", "Droid Sans Mono", "Comic Sans MS");
<Label x:Name="Label1" Text="" HorizontalOptions="Center" VerticalOptions="CenterAndExpand"> <Label.FontFamily> <OnPlatform x:TypeArguments="String"> <On Platform="Android">Droid Sans Mono</On> </OnPlatform> </Label.FontFamily> </Label>
@Othello7 said:
I tried it, but it did this:
I tried debugging anyway and it wasn't monospace.
I'm using Visual Studio 2017, USB debugging on a KitKat 4.4 tablet if that is any help.
I suggest raising a ticket with Xamarin Support and sending them your entire solution. For how to do this, see https://forums.xamarin.com/discussion/150917/the-microsoft-xamarin-support-team-is-here-to-help#latest
If you get an answer, do share it here in case it is useful to other people.
Ok! It was pretty much that i had tried too many alternate ways to set the font and after some commenting out of code they did, this worked. And others like it.<Label.FontFamily> <OnPlatform x:TypeArguments="x:String"> <On Platform="iOS" Value="CourierNewPSMT" /> <On Platform="Android" Value="monospace" /> <On Platform="UWP" Value="Courier New" /> </OnPlatform> </Label.FontFamily>
Also, using the custom font worked.
Thank you to everyone on these forums and to microsoft support for helping me with my problem.
Answers
Can you not use x:OnPlatform markup extension?
in a style you could set it like this
And then just set your label style etc in your theme
On Android, to get a fixed-width font, try "monospace" rather than "Droid Sans Mono" when setting the FontFamily property.
It isn't.
One of the first things I did when I started working with Xamarin.Forms was to create a page on which I could select from a list of font families, toggle a bold button, toggle an italics button, and have a Label update to reflect those settings. I still have that page and it allows me to change the FontFamily as many times as I want, with the change being reflected in the Label. I obtain the list of font families from a DependencyService that I implemented, with each platform providing a different list of font families (I've been suggesting to Xamarin that they provide some fonts that work cross-platform, but so far to no avail).
To use custom fonts in Xamarin.Forms (Droid project):
Define the fonts for each OS.
Reference the key use the font .
Tutorials about using custom fonts you can refer to:
https://xamarinhelp.com/custom-fonts-xamarin-forms/
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/fonts#use-a-custom-font
Thanks for the help, but I still cant seem to make it work.
I tried NMackay's solution. I had not heard of:
I don't really need the other platforms just yet, I am just trying to make it work on android
But It didn't seem to work for me.
So then I took JohnHardman's advice:
But it still came up with the default font.
I have already tried things along the lines of this (from NMackay):
I had before also tried Jarvan's suggestion of using a custom font, but it didn't do anything the way I did it then.
But I saw the code was a little different so I tried it again and got an exception:
System.InvalidOperationException: Cannot determine property to provide the value for.
So I really don't know what to do. I felt like I was banging my head against a wall, going around, looking at a lot of forums, trying everything. It is great to be able to get help from people who know what they're doing.
@Othello7
Try:
Here is the basic sample file which works fine on my side, you can refer to it.
I tried it, but it did this:

I tried debugging anyway and it wasn't monospace.
I'm using Visual Studio 2017, USB debugging on a KitKat 4.4 tablet if that is any help.
I tried running it, it didn't work on my end. I got this exception:
it was on line 22 in MainActivity.cs
base.OnCreate(savedInstanceState);
Android 5.0 (API 21) is recommended as the minimum API, lower versions will cause full compatibility of the Android support libraries. Try to deploy the code on SDK 5 or higher.
https://docs.microsoft.com/en-us/xamarin/get-started/requirements?tabs=windows#target-platforms
The Minimum API wouldn't prevent monospace working for a Label on Android 4.4 (it might cause other problems though). I've just tested on 4.2.2 running on a Huawei phone and it worked fine. The Minimum API should be taken into account when targeting devices, but it I wouldn't expect it to be causing a problem with FontFamily on Android.
I would like for my apps to be able to work on my old tablet, KitKat 4.4, API 19. I guess if I'm forced I can switch to newer, but if its at all possible I would use the older version.
It seems not to recognize
x:TypeArguments
for me, and have trouble with the On Platform stuff sometimes (none of which have actually worked). Originally I was just going to make an android xamarin app. But I discovered that there wasn't much beginners tutorials for the android specific project. So I switched to a xamarin cross-platform project which also might be beneficial later on.My computer does not recognize my android hardware, So I don't know what model tablet I am using.
My setting in the App.Android look just like the image in the microsoft info @NMackay sent

Everything else is mostly default settings.
Kitkat doesn't support SSL etc (well without iffy hacks), it's not secure. if your app is for non store I'd understand
I suggest raising a ticket with Xamarin Support and sending them your entire solution. For how to do this, see https://forums.xamarin.com/discussion/150917/the-microsoft-xamarin-support-team-is-here-to-help#latest
If you get an answer, do share it here in case it is useful to other people.
Ok! It was pretty much that i had tried too many alternate ways to set the font and after some commenting out of code they did, this worked. And others like it.
<Label.FontFamily> <OnPlatform x:TypeArguments="x:String"> <On Platform="iOS" Value="CourierNewPSMT" /> <On Platform="Android" Value="monospace" /> <On Platform="UWP" Value="Courier New" /> </OnPlatform> </Label.FontFamily>
Also, using the custom font worked.
Thank you to everyone on these forums and to microsoft support for helping me with my problem.