Reputation: 12651
I'm trying to apply Google's Quicksand font (https://fonts.google.com/?query=quicksand) to my Xamarin Android app.
I've added quicksand_bold.ttf
, quicksand_light.ttf
, quicksand_medium.ttf
and quicksand_regular.ttf
to the Resources\font
folder, with a build type of AndroidResource.
I have also added a file called quicksand.xml
to the same folder. This contains the text
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<font android:font="@font/quicksand_regular"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/quicksand_regular"
app:fontStyle="normal"
app:fontWeight="400" />
<font android:font="@font/quicksand_bold"
android:fontStyle="normal"
android:fontWeight="700"
app:font="@font/quicksand_bold"
app:fontStyle="normal"
app:fontWeight="700" />
<font android:font="@font/quicksand_medium"
android:fontStyle="normal"
android:fontWeight="500"
app:font="@font/quicksand_medium"
app:fontStyle="normal"
app:fontWeight="500" />
<font android:font="@font/quicksand_light"
android:fontStyle="normal"
android:fontWeight="300"
app:font="@font/quicksand_light"
app:fontStyle="normal"
app:fontWeight="300" />
</font-family>
</font-family>
And finally I've applied the theme Theme="@style/MyTheme"
to my main Activity:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="@android:style/Theme.Holo">
<item name="android:fontFamily">@font/quicksand</item>
</style>
</resources>
I've been following the instructions at https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/resources-in-android/fonts. I'm assuming that the @font/quicksand
element directs the system to the quicksand.xml
file.
I would have thought that these steps would be sufficient for the font to be picked up as part of my default style, but this isn't happening.
Am I missing a step?
Upvotes: 0
Views: 647
Reputation: 11601
Using custom fonts can be tricky in Android, it can cause big RAM usage.
One of the easiest way to add custom font to your app is using Calligraphy-xamarin which is Xamarin binding of Calligraphy.
Here is quick tutorial:
First Install-Package CallygraphyXamarin
, then add following to your Activity
protected override void AttachBaseContext(Context newBase)
{
base.AttachBaseContext(CalligraphyContextWrapper.Wrap(newBase));
}
And finally use custom font like so:
<TextView
fontPath="fonts/myfont.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world!" />
If you want to set default font for your app, add (for example) MyApplication.cs
to your project with following content:
[Application]
public class MyApplication : Application
{
/// <inheritdoc />
public MyApplication(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
/// <inheritdoc />
public override void OnCreate()
{
base.OnCreate();
CalligraphyConfig.InitDefault(
new CalligraphyConfig.Builder()
.SetDefaultFontPath("fonts/myfont.ttf")
.SetFontAttrId(Resource.Attribute.fontPath)
.Build()
);
}
}
Upvotes: 1