michael__sloan
michael__sloan

Reputation: 425

How do I use a custom font in Xamarin.Android?

I understand how to add a .tff file into my project but I haven't been able to find a post that makes it clear how and where to implement the font once it's been added. I found a post that says to implement the font like this:

var typeface = Typeface.CreateFromAsset (context.Assets, fileName);

But I don't know where I should add this line of code, or what I should use in place of context. Can someone give me a basic explanation so I know what I'm missing?

Upvotes: 4

Views: 10687

Answers (4)

El0din
El0din

Reputation: 3370

Just to be generic, and use as style, you can do this in the .axml (the font need to be into "Resources/Font/PermanentMarker.ttf"):

 <TextView
        ...
        android:fontFamily="@font/permanentmarker"/>

Upvotes: 0

SushiHangover
SushiHangover

Reputation: 74209

Using a template created Xamarin.Android single Activity application:

Add a font to the Assets directory with a build type of AndroidAsset:

├── Assets
│   ├── AboutAssets.txt
│   └── Jellee-Roman.ttf

In the OnCreate add the following:

Button button = FindViewById<Button>(Resource.Id.myButton);

// Add these two lines:
var font = Typeface.CreateFromAsset(Assets, "Jellee-Roman.ttf");  
button.Typeface = font;

button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };

Upvotes: 13

Jon Douglas
Jon Douglas

Reputation: 13176

Once you have the reference to the Typeface, you can then set it into the TextView via the following:

Typeface property - https://developer.xamarin.com/api/property/Android.Widget.TextView.Typeface/

SetTypeface method - https://developer.xamarin.com/api/member/Android.Widget.TextView.SetTypeface/p/Android.Graphics.Typeface/Android.Graphics.TypefaceStyle/

Upvotes: 1

amitairos
amitairos

Reputation: 2967

If you want to change the font of a textview, then in your OnCreate method put this after you do FindViewById:

var typeface = Typeface.CreateFromAsset(Assets, "filename.ttf");
myTextView.TypeFace = typeface;

Upvotes: 0

Related Questions