user2660030
user2660030

Reputation:

error in changing typeface in xamarin for android

I want to change font of my textview and I follow the code given in tutorials and samples. But I am getting error in it. my code is

var txt = FindViewById<TextView> (Resource.Id.textView1);
        Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/k010.ttf");
        txt.SetTypeface (tf, TypefaceStyle.Normal);

I am getting error in 2nd line the error says

The object refrance is required for non static field,method

I want to use kruti dev 10 font in my textview.

Upvotes: 3

Views: 4250

Answers (2)

Vikas Rana
Vikas Rana

Reputation: 1999

Change

Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/k010.ttf");

to

Typeface tf = Typeface.CreateFromAsset (Application.Context.Assets, "fonts/k010.ttf");

Upvotes: 7

Karan Maru
Karan Maru

Reputation: 1001

please change

Typeface tf = Typeface.CreateFromAsset (getApplicationContext().getAssets(), "fonts/k010.ttf");

to

Typeface tf = Typeface.CreateFromAsset (this.getAssets(), "fonts/k010.ttf");

please refer below link for better solution regarding this.

https://github.com/xamarin/monodroid-samples/blob/master/ApiDemo/Graphics/Typefaces.cs

Upvotes: 1

Related Questions