0

I have the following XAML code:

<Label Text="Hello Forms with XAML">
        <Label.FontFamily>
            <OnPlatform x:TypeArguments="x:String">
                <OnPlatform.iOS>Roboto-Light</OnPlatform.iOS>
                <OnPlatform.Android>Roboto-Light.ttf#Roboto-Light</OnPlatform.Android>
                <OnPlatform.WinPhone>Assets/Fonts/Roboto-Light.ttf#Roboto-Light</OnPlatform.WinPhone>
            </OnPlatform>
        </Label.FontFamily>
    </Label>

However I get the error "Java.Lang.RuntimeException: Font asset not found Roboto-Light" even though the font is placed in the Assets folder:

Solution Explorer

What can I do to fix this?

Radu
  • 584
  • 1
  • 6
  • 30
  • Right-click , go to properties and check the Build Action. It should also have something with 'asset' in it. Also, I think Android doesn't like the hyphens ('-' sign) too much. Try removing it. – Gerald Versluis Jul 14 '17 at 10:34
  • Build Action is AndroidAsset – Radu Jul 14 '17 at 10:36

2 Answers2

1

On Android you must ensure that the part after the # is the actual name of the font. This part is NOT directly the same as the file name. I also use Roboto in one of my apps and it's declared as:

<OnPlatform.Android>fonts/Roboto-Regular.ttf#Roboto</OnPlatform.Android>
<OnPlatform.Android>fonts/Roboto-Bold.ttf#Roboto Bold</OnPlatform.Android>

You might want to try yours as:

<OnPlatform.Android>Roboto-Light.ttf#Roboto Light</OnPlatform.Android>

You can find out this name on Mac by selecting the font in FontBook and checking its Full Name property.

enter image description here

Steven Thewissen
  • 2,971
  • 17
  • 23
  • I just did that and the exact same error appeared: Java.Lang.RuntimeException: Font asset not found Roboto-Light.ttf – Radu Jul 14 '17 at 10:51
1

You can find a comprehensive guide to using custom fonts in Xamarin.Forms at this Xamarin Developers Guide Link.

As Steven highlighted, it's important to ensure that your font ttf file is in the right place, in this instance you appear to have got it right, the location for the android project is indeed the 'assets' folder.

Xamarin.Forms for Android can reference a custom font that has been added to the project by following a specific naming standard. First add the font file to the Assets folder in the application project and set Build Action: AndroidAsset.

Setting a custom font in C# backing classes, snippet amended from the above source:

public class SomeMethod()
{
    new Label
    {
      Text = "Hello, Forms!",
      FontFamily = Device.OnPlatform(null, "Roboto-Light.ttf#Roboto-Light", null)
    }
}

The xaml should actually be:

<Label Text="Hello Forms with XAML">
    <Label.FontFamily>
        <OnPlatform x:TypeArguments="x:String">
            <On Platform="Android">Roboto-Light.ttf#Roboto-Light</On>
        </OnPlatform>
    </Label.FontFamily>
</Label>

It is worth mentioning that the roboto theme should be part of the native android project already if your targeting API 14 or above. So on android you can actually just use the 'sans-serif-light' font which is infact Roboto. Follow this link to see an excellent answer on an android specific thread about the fonts they now include in API 14+

JoeTomks
  • 3,243
  • 1
  • 18
  • 42
  • Definetely handy information. Thanks! I can't mark it as best answer, though, because it still didn't fix the initial problem. – Radu Jul 14 '17 at 16:40