Joel Hernández
Joel Hernández

Reputation: 115

Personalized font using Android 5 or higher - XML

I am following this tutorial to change the fonts of the app in Xamarin.Android, it is just adding the .ttf file to the project and then referring to the TextView / EditText where you want to use it in the XML, like this:

android:fontFamily="@font/sourcerfont"

The problem I am having is that it only works with virtual devices, and not with physical ones.

I reread the documentation (the tutorial) and found this part that caught my attention, and I get the impression that the error could be there since my physical device has API 23 - Android 6.0.1; and the virtual one has Android 9.0:

This feature is also supported on devices till Android 4.1 by using Support Library 26 or more.

enter image description here

Could you tell me what I'm doing wrong?

I don't think you can only use custom fonts on Android apps with versions older than 8.

The only way that works for me on all devices is this but it is very tedious with large apps:

TextView Lbl = FindViewById <TextView> (Resource.Id.Lbl34);
            tf = Typeface.CreateFromAsset (Android.App.Application.Context.Assets, "quicks.ttf");
            Lbl.SetTypeface (tf, TypefaceStyle.Normal);

Upvotes: 2

Views: 96

Answers (1)

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10356

I reread the documentation (the tutorial) and found this part that caught my attention, and I get the impression that the error could be there since my physical device has API 23 - Android 6.0.1; and the virtual one has Android 9.0

I download Karantina font family, then creating a folder named font under the Resources folder. Put the font file inside it, build action is set to AndroidResource , see the following screenshot:

enter image description here

I use this custom font:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<TextView
   android:text="My text content"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
 android:fontFamily="@font/karantinaregular" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="karantina Bold" 
    android:fontFamily="@font/karantinabold"/>
 <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="karantina Light" 
    android:fontFamily="@font/karantinalight"/>

I test my code on Android virtual 8.1(API27), Android physical device 8.1(API27) and Android physical device 6.0(API23), it all works, having no problem.

Upvotes: 1

Related Questions