Enhancing Android Performance Using Icon Fonts
How to integrate Icon fonts
Section titled “How to integrate Icon fonts”In order to use icon fonts, just follow the steps below:
](https://i.stack.imgur.com/nVmeb.png)
public class FontManager { public static final String ROOT = "fonts/"; FONT_AWESOME = ROOT + "myfont.ttf"; public static Typeface getTypeface(Context context) { return Typeface.createFromAsset(context.getAssets(), FONT_AWESOME); }}You may use the Typeface class in order to pick the font from the assets. This way you can set the typeface to various views, for example, to a button:
Button button=(Button) findViewById(R.id.button);Typeface iconFont=FontManager.getTypeface(getApplicationContext());button.setTypeface(iconFont);Now, the button typeface has been changed to the newly created icon font.
.icon-arrow-circle-down:before { content: “\e001”;}.icon-arrow-circle-left:before { content: “\e002”;}.icon-arrow-circle-o-down:before { content: “\e003”;}.icon-arrow-circle-o-left:before { content: “\e004”;}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:
<resources> <! — Icon Fonts --> <string name=”icon_arrow_circle_down”> </string> <string name=”icon_arrow_circle_left”> </string> <string name=”icon_arrow_circle-o_down”> </string> <string name=”icon_arrow_circle_o_left”> </string></resources>button.setText(getString(R.string.icon_arrow_circle_left))TabLayout with icon fonts
Section titled “TabLayout with icon fonts”public class TabAdapter extends FragmentPagerAdapter {
CustomTypefaceSpan fonte;List<Fragment> fragments = new ArrayList<>(4);private String[] icons = {"\ue001","\uE002","\uE003","\uE004"};
public TabAdapter(FragmentManager fm, CustomTypefaceSpan fonte) { super(fm); this.fonte = fonte for (int i = 0; i < 4; i++){ fragments.add(MyFragment.newInstance()); }}
public List<Fragment> getFragments() { return fragments;}
@Overridepublic Fragment getItem(int position) { return fragments.get(position);}
@Overridepublic CharSequence getPageTitle(int position) { SpannableStringBuilder ss = new SpannableStringBuilder(icons[position]); ss.setSpan(fonte,0,ss.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); ss.setSpan(new RelativeSizeSpan(1.5f),0,ss.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE ); return ss;}
@Overridepublic int getCount() { return 4;}
}- In this example, myfont.ttf is in Assets folder. Creating Assets folder
- In your activity class
//..TabLayout tabs;ViewPager tabs_pager;public CustomTypefaceSpan fonte;//..
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //... fm = getSupportFragmentManager(); fonte = new CustomTypefaceSpan("icomoon",Typeface.createFromAsset(getAssets(),"myfont.ttf")); this.tabs = ((TabLayout) hasViews.findViewById(R.id.tabs)); this.tabs_pager = ((ViewPager) hasViews.findViewById(R.id.tabs_pager)); //...}
@Overrideprotected void onStart() { super.onStart(); //.. tabs_pager.setAdapter(new TabAdapter(fm,fonte)); tabs.setupWithViewPager(tabs_pager); //..Remarks
Section titled “Remarks”Icon Fonts are like normal font types that have symbols instead of letters. It can be used in your application with at-most ease.
They are:
- Flexible
- Scalable
- Vectors
- Fast Processable
- Light Weight
- Accessible
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.
