Setting multiple fonts in Android is quite a pain in the ass. Until Android 4.2 there is no way to specify different font families in a Layout, and doing it from code makes us setup the font control by control, and we have to perform findViewById for all controls even when not needed, resulting in a degradation of performance.

Dealing with this, I’ve tried to reimplement the inflation process, but at the end this resulted in slower apps, and some components like action bar gave me some problems. (This was explained in a previous post, but some #$%@ hacker decided that his/her/its stupid message was more important than my post).
I’ve came to a faster solution, not ideal, but faster to implement and use.
Basically I’ve created a wrapper for all controls that implemented text (TextView, EditText, etc) that used the attribute tag to setup the font.

Something like this:

And the code for the FancyTextView:

Then, we only need to copy the appropiate fonts to assets folder.

Pending improvements:

  • Detect if the font is not available and failsafe to default font
  • Create a hashmap to store the Typeface objects to avoid multiple instantiation (that could make a difference on big layouts)
  • Change filenames by alias using a string array as resource, so we can map them and change at once if required.
 

One Response to Settings fonts in Android

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">