Fun with Fonts PDF Print E-mail

Inevitably, you’ll get the question “hey, can we change this font?” when doing application development. The answer depends on what fonts come with the platform, whether you can add other fonts, and how to apply them to the widget or whatever needs the font change.

 

Android is no different. It comes with some fonts plus a means for adding new fonts. Though, as with any new environment, there are a few idiosyncrasies to deal with.

Android natively knows three fonts, by the shorthand names of “sans”, “serif”, and “monospace”. These fonts are actually the Droid series of fonts, created for the Open Handset Alliance by Ascender.

For those fonts, you can just reference them in your layout XML, if you choose, such as:

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <TableLayout 
  3. xmlns:android="http://schemas.android.com/apk/res/android" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="fill_parent" 
  6. android:stretchColumns="1"> 
  7. <TableRow> 
  8. <TextView 
  9. android:text="sans:" 
  10. android:layout_marginRight="4px" 
  11. android:textSize="20sp" 
  12. /> 
  13. <TextView 
  14. android:id="@+id/sans" 
  15. android:text="Hello, world!" 
  16. android:typeface="sans" 
  17. android:textSize="20sp" 
  18. /> 
  19. </TableRow> 
  20. <TableRow> 
  21. <TextView 
  22. android:text="serif:" 
  23. android:layout_marginRight="4px" 
  24. android:textSize="20sp" 
  25. /> 
  26. <TextView 
  27. android:id="@+id/serif" 
  28. android:text="Hello, world!" 
  29. android:typeface="serif" 
  30. android:textSize="20sp" 
  31. /> 
  32. </TableRow> 
  33. <TableRow> 
  34. <TextView 
  35. android:text="monospace:" 
  36. android:layout_marginRight="4px" 
  37. android:textSize="20sp" 
  38. /> 
  39. <TextView 
  40. android:id="@+id/monospace" 
  41. android:text="Hello, world!" 
  42. android:typeface="monospace" 
  43. android:textSize="20sp" 
  44. /> 
  45. </TableRow> 
  46. <TableRow> 
  47. <TextView 
  48. android:text="Custom:" 
  49. android:layout_marginRight="4px" 
  50. android:textSize="20sp" 
  51. /> 
  52. <TextView 
  53. android:id="@+id/custom" 
  54. android:text="Hello, world!" 
  55. android:textSize="20sp" 
  56. /> 
  57. </TableRow> 
  58. </TableLayout> 

This layout builds a table showing short samples of four fonts. Notice how the first three have the android:typeface attribute, whose value is one of the three built-in font faces (e.g., “sans”).

The three built-in fonts are very nice. However, it may be that a designer, or a manager, or a customer wants a different font than one of those three. Or perhaps you want to use a font for specialized purposes, such as a “dingbats” font instead of a series of PNG graphics.

The easiest way to accomplish this is to package the desired font(s) with your application. To do this, simply create an assets/ folder in the project root, and put your fonts (in TrueType, or TTF, form) in the assets. You might, for example, create assets/fonts/ and put your TTF files in there.

Then, you need to tell your widgets to use that font. Unfortunately, you can no longer use layout XML for this, since the XML does not know about any fonts you may have tucked away as an application asset. Instead, you need to make the change in Java code:

 
  1. public class FontSampler extends Activity { 
  2. @Override 
  3. public void onCreate(Bundle icicle) { 
  4. super.onCreate(icicle); 
  5. setContentView(R.layout.main); 
  6.  
  7. TextView tv=(TextView)findViewById(R.id.custom); 
  8. Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf"); 
  9.  
  10. tv.setTypeface(face); 

Here, we grab the TextView for our “custom” sample, then create a Typeface object via the static createFromAsset() builder method. This takes the application’s AssetManager (from getAssets()) and a path within your assets/ directory to the font you want.

Then, it is just a matter of telling the TextView to setTypeface(), providing the Typeface you just created. In this case, we are using the Handmade Typewriter font.

The results?

Font Sampler

Note that Android does not seem to like all TrueType fonts. I spent a lot of time trying to figure out what I was doing wrong with this sample, before switching to a different font, and everything “just worked”. When Android dislikes a custom font, rather than raise an Exception, it seems to substitute Droid Sans (”sans”) quietly. So, if you try to use a different font and it does not seem to be working, it may be that the font in question is incompatible with Android, for whatever reason.

Also note that TrueType fonts can be rather pudgy, particularly if they support an extensive subset of the available Unicode characters. The Handmade Typewriter font used here runs over 70KB; the DejaVu free fonts can run upwards of 500KB apiece. Even compressed, these add bulk to your application, so be careful not to go overboard with custom fonts, lest your application take up too much room on your users’ phones.

 

Source: http://androidguys.com/2008/08/18/fun-with-fonts/

Comments (25)Add Comment
Michel Jordan
written by Michel Jordan , May 20, 2010
Michel Jordan
Fashion Nike Jordans
report abuse
vote down
vote up
Votes: +0
Thanks
written by aol stock quote , May 21, 2010
Thank you so much for sharing the information with us.
report abuse
vote down
vote up
Votes: +0
tiffany jewelry
written by tiffany jewelry , May 22, 2010
tiffany jewelry The Handmade Typewriter font used here runs over 70KB; the DejaVu free fonts can run upwards of 500KB apiece. Even compressed, these add bulk to your application, so be careful not to go overboard with custom fonts, lest your application take up too much room on your users’ phones.
report abuse
vote down
vote up
Votes: +0
cheap nfl jerseys
written by cheap nfl jerseys , May 24, 2010
cheap nfl jerseys
nfl football jerseys
report abuse
vote down
vote up
Votes: +0
air jordan
written by air jordan , May 24, 2010
air jordan
air jordan shoes
report abuse
vote down
vote up
Votes: +0
air jordan shoes
written by air jordan shoes , May 24, 2010
air jordan shoes
jordan shoes
report abuse
vote down
vote up
Votes: +0
cheap EQ2 Gold
written by cheap EQ2 Gold , May 27, 2010
Gamers ought to know Ever Quest 2 Gold if they really like to play this game. All gamers want to Buy Ever Quest 2 Evil to promote their character level easily.
report abuse
vote down
vote up
Votes: +0
birkenstock shoes
written by birkenstock , May 29, 2010
When you are on a holiday in some seaside resort where you will hike and walk on cliffs, Birckenstock is one of the brands to which you turn your attention to find some slippers or flip-flops that make a holiday in comfort and without pain in the feet after a romantic walk on the rocks.Birkenstocks is the well-known German footwear brand: Birkenstock shoes, Birkenstock sandals and Birkenstock clogs.Birkenstock Gizeh is a flattering flip-flop style with a leather upper that will look great with shorts, jeans or dresses, at the same time, it has an irresistible air between the tourist and hiking, and you can be sure that the evening will have no pain, given the high quality these shoes.
report abuse
vote down
vote up
Votes: +0
...
written by supra shoes , May 31, 2010
The are popular with both recreation and Supra Shoes
Supra Footwear sportswear are simply matchless in terms of style, comfort and longevity, Now! Free Shipping! NO Tax!
report abuse
vote down
vote up
Votes: +0
thanks
written by how to get bigger biceps , June 03, 2010
how to get bigger biceps Looking forward for your next share.
report abuse
vote down
vote up
Votes: +0
Air jordans shoes
written by Air jordans shoes , June 03, 2010

jordan retro iii jordan retro iii jordan retro iii
http://www.mydiscountairjordanshoes.com/
jordan retro iii jordan retro iii jordan retro iii
report abuse
vote down
vote up
Votes: +0
english
written by fancy1233 , June 04, 2010
The world Vinylis filled with unlikely flexfriendships. How did they begin, with one flex bannerperson desperately tarpaulinin need and another willing to lend a helping hand. When meshsuch kindness is offered, we are finally able to see the worthone way vision of those we have previously written off, and before we've known it, blockout a bond has formed, regardless of whether others can understand it. Yes, unlikely friendships start up everyday, no one understands thispvc fabric more
than the lonely, in fact, and it's what they count on.

This isflex how Bree Van De Kamp finallyVinylcame to change her weekly routine, sheflex banner still cleaned on Tuesdays, paid her bills on Wednesdays, and did her tarpaulinlaundries onThursdays, butone way vision her Fridays were now reserved for a meeting, mesha special meetingblockout where she stood in front of people she didn't know,and pvc fabricsaid things she didn't believe, and afterwards Bree would come
home and rewardpvc adhesive vinyl herself on the completion of another successful week.












report abuse
vote down
vote up
Votes: +0
...
written by louis vuitton monogram canvas , June 17, 2010
Good post, I want to post opinions since the comments provides web owners for being more engaged and towards the chance to certainly gain knowledge of from each other.
report abuse
vote down
vote up
Votes: +0
...
written by authentic jordans , June 18, 2010

thnaks for your sahre!!!!!!!!!!!!
report abuse
vote down
vote up
Votes: +0
...
written by mbt , June 18, 2010
thnaks for your sahre
report abuse
vote down
vote up
Votes: +0
Though,...
written by juicy couture tracksuit , June 19, 2010
Though, as with any new environment, there are a few idiosyncrasies to deal with.
report abuse
vote down
vote up
Votes: +0
new bakugan
written by new bakugan , June 28, 2010
bakugan toys
Unazukin
report abuse
vote down
vote up
Votes: +0
mbt
written by mbt shoes , June 29, 2010
at the same time, it has an irresistible air between the tourist and hiking, and you can be sure that the evening will have no pain, given the high quality these shoes.
report abuse
vote down
vote up
Votes: +0
aa
written by DC GEAR MOTOR , June 29, 2010
I really appreciate what you’re doing here.
report abuse
vote down
vote up
Votes: +0
qq
written by wholesale wedding dresses , June 29, 2010
It's time consuming to discover an affordable web site designer that can actually develop web sites of good quality. Come check us outif your need an awesome website!
report abuse
vote down
vote up
Votes: -1
fonts are the ish
written by athlean x , June 30, 2010
fonts help me with my graphic design projects to the max. thanks for this
report abuse
vote down
vote up
Votes: -2
...
written by Buy Resume , July 07, 2010
Thank you, your article is incredible
report abuse
vote down
vote up
Votes: +1
...
written by sexy lady shoes , July 08, 2010
Here you are .You can buy what you want ,for example ,
chrstian shoes
louboutin shoes ,mieshi .
report abuse
vote down
vote up
Votes: +0
Kool.
written by logo design , July 08, 2010
Here you are, thanks
report abuse
vote down
vote up
Votes: +0
answer this topic
written by BuckleyGuadalupe , July 13, 2010
Different people in all countries get the personal loans in different creditors, because that is comfortable.
report abuse
vote down
vote up
Votes: +0

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

security code
Write the displayed characters


busy
 

 

This domain is for sale! Please contact me via contact page!