Font Awesome Icons in Android

Here's a small example of how to use Font Awesome in your Android apps. First of all, download font-awesome and place the fontawesome-webfont.ttf file in your assets folder. Then, add to your strings.xml file the following definitions:



Now, you'll need to set the Typeface for each widget you're going to put an icon, for example, a Button:



Since it's a font, you can control the color and size of the icons programmatically and the icons will scale automatically. The biggest downside is that the icons won't appear in the graphical layout.


Icons in the ActionBar

You can place your icons in pretty much any part of the ActionBar title or in the tabs.

Tabs





ActionBar Icon

You can also customize the ActionBar title with a very cool hack. First, you'll need to create a MetricAffectingSpan like this one:



Then you change the ActionBar title on your activity onCreate method:



The result is the following:




Generate the XML

Run the following against the variables.less file in Font Awesome:

grep "\f" variables.less | awk -F\" '{ print $1 $2 ";</string>" }' | sed 's/\\/\&#x/' | sed 's/@/<string name=\"icon_/' | sed 's/:\ /\">/' | sed 's/-/_/g'


No hay comentarios:

Publicar un comentario