Enhancing Android Performance Using Icon Fonts
Icon Fonts are like normal font types that have symbols instead of letters. It can be used in your application with at-most ease.
- Fast Processable
- Light Weight
Effect on Size
Exporting an image in various sizes for android devices would cost your app, additional asset size of around 30kB per image. While adding a font file(.ttf) with around 36 icons would cost just 9kB. Just imagine the case if you are adding 36 individual files of various configurations it would be around 1000kB. It’s a reasonable amount of space that you will save by using icon fonts.
Limitations of Icon fonts.
Icon fonts can be used in navigation drawer. Using them in navigation views as icon of menu items is not possible as the menu file cannot be created without specifying the title. So it is advisable to use svg files as resources for these icons.
Icon fonts cannot be used in floating action button. as they does not have a
External fonts cannot be applied from xml. They must be specified using the java file. Or else you need to extend the basic view and create a view as specified in this post
How to integrate Icon fonts
In order to use icon fonts, just follow the steps below:
Add the font file to your project
You may create your font icon file from online websites such as icomoon, where you can upload SVG files of the required icons and then download the created icon font. Then, place the .ttf font file into a folder named fonts (name it as you wish) in the assets folder:
Create a Helper Class
Now, create the following helper class, so that you can avoid repeating the initialisation code for the font:
You may use the
Typefaceclass in order to pick the font from the assets. This way you can set the typeface to various views, for example, to a button:
Now, the button typeface has been changed to the newly created icon font.
Pick up the icons you want
Open the styles.css file attached to the icon font. There you will find the styles with Unicode characters of your icons:
This resource file will serve as a dictionary, which maps the Unicode character associated with a specific icon to a human-readable name. Now, create the string resources as follows:
Use the icons in your code
Now, you may use your font in various views, for example, as follows:
You may also create button text views using icon fonts:
TabLayout with icon fonts
- In this example, myfont.ttf is in Assets folder. Creating Assets folder
- In your activity class