Log in or register to post comments

How to change TextView font? (ImageTargets)

October 27, 2012 - 8:57am #1

 

I've added TextView in GUIManager.java - initButton.

Usually, To use external fonts,

Textview tv = (TextView) overlayView.findViewById(R.id.textviewID);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/fontNAME.ttf");
tv.setTypeface(tf);
 
.....and add .ttf file in assets/fonts.

 

But I can't use external font... (NULLPointException occurs getAssets().)

How to use external fonts in this case?

How to change TextView font? (ImageTargets)

October 31, 2012 - 8:27am #5

You're welcome!

How to change TextView font? (ImageTargets)

October 31, 2012 - 8:26am #4

Thank you!

solved the problem!
The solve is very simple...

As you said,  I had to import the context.

Context  has declared in ImageTargets :

 // The main application context
 private static Context applicationContext;

 

So I added this:

Typeface tf = Typeface.createFromAsset(applicationContext.getAssets(), "fonts/fontNAME.ttf");

 

Thanks so much:)

How to change TextView font? (ImageTargets)

October 30, 2012 - 6:27am #3

Hi, I did the following test (using a Button instead of a TextView, but that does not really matter):

in the Dominoes sample, in the  initButton() method of GUIManager, right after the code that creates the "Clear" button,

I added the following code:

  Typeface tf = Typeface.createFromAsset(mAppContext.getAssets(), "fonts/ariali.ttf");

  clearButton.setTypeface(tf);

Where mAppContext is just the context passed to the GUIManager in the GUIManager constructor (I store it in a private varaiable mAppContext)

and where ariali.ttf is a sample font that I downlaoded from internet and added to the "assets/fonts/" folder (same as you did);

the test shows that it works all right, the text on the button is actually rendered with italic arial (in my case) and no Null Pointer;

 

One thing to check: try maybe with a different font; also, make sure to understand if it is getAssets() returning NULL or the createFromAsset method;

finally, please note that the font name is CASE SENSITIVE (i.e. it might fail because of that, so double check your font name in your code).

I hope this helps.

A.

 

How to change TextView font? (ImageTargets)

October 29, 2012 - 5:54am #2

Hi, I'll have a look into this;

by the way, do you get Null pointer exception in the getAssets() function, or in the createFromAsset function ?

Log in or register to post comments