Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Change font in Android

HI, I would just like to know how do you change font in Android and use it with most of the TextViews in the display?

1 Answer

Hi Zainal

The are 2 ways, the quickest way (and if it is only for 1 or 2 instances) is to create a new Typeface object and set the typeface to a font file inside your assets folder. (Example code below)

TextView myTextView = (TextView) findViewById(R.id.textview1);
Typeface customFont = Typeface.createFromAsset(getAssets(),"fonts/customfont.ttf"); 
//where inside you assets folder you have created a subfolder named fonts and placed a file name customfont.ttf inside
myTextView.setText("My Custom Font");
myTextView.setTypeFace(customFont);

The second way would involve creating a custom class that extends TextView and defining the TypeFace in that custom class. You can then reference this new class inside your xml file or probably more useful is to define it as a custom style which you can easily apply it to all of your textview (if you need that of course).

Hope this helps

Daniel