Android: Using a custom font
Can you set a custom font from XML? As far as I’ve managed to find, the answer is no.
So instead, how do you do this programatically?
First start by creating an ‘assets’ folder in your project, within this create a folder called ‘fonts’. ‘assets’ should be on the same level as ‘res’ and not within it. Drag your font file in here. Need a font? Lots of free, open source ones available here: http://www.google.com/webfonts/v2.
Setting the font on a View couldn’t be simpler, here’s an example using the Lobster font from Google webfonts
TextView someTextView = (TextView)findViewById(R.id.some_text_view_id); Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Lobster.ttf"); someTextView.setTypeface(tf);
If needing to set the font in a list item, I load the font to a member variable within the constructor of my ListAdapter so that it can be used in my overridden newView() rather than loading it every time.