Radu
Radu

Reputation: 626

Java.Lang.RuntimeException: Font asset not found Roboto-Light (Beginner)

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?

Upvotes: 0

Views: 3481

Answers (2)

JoeTomks
JoeTomks

Reputation: 3276

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+

Upvotes: 1

Steven Thewissen
Steven Thewissen

Reputation: 2981

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

Upvotes: 1

Related Questions