This is Codecumentation! Simple-code Documentation based on Real Implementation


How to use icons and symbols from fonts on Android

I think it's an alternate way to reduce some resources on android development, yep bundle all icon as .ttf file (font ext). Sure it really hacky and customizable, you can change icon size by textSize change color by textColor and an another TextView syntax.

For example you can try to code with well known font, Font Awesome.

  • Copy your font (I.e. font awesome) fontawesome-webfont.ttf into assets folder, or for spesific create new directory called fonts, it will be assets/fonts.

  • Found the character entities for icons you want, if it font awesome you can use this page: http://fortawesome.github.io/Font-Awesome/cheatsheet/

  • Created an entry in strings.xml for each icon. (E.g. code)
    fa-code []
  • <string name="icon_code">&#xf121;</string>
    

  • Then load on xml layout:
  • <TextView
       android:id="@+id/icCode"
       android:gravity="center"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textSize="50dp"
       android:textColor="@color/white"
       android:text="@string/icon_code" />
    

  • Set font as appropriate Views by load it on onCreate method:
  • Typeface font = Typeface.createFromAsset( getAssets(), "fonts/fontawesome-webfont.ttf" );
            TextView tvCode = (TextView)findViewById(R.id.icCode);
            tvCode.setTypeface(font);
    

That's all, like the post title now you can load icon or symbol through included font. Overall, If it can be reduce resources but it still be time consuming.

Warning : Memory leak constantly creating new typeface   https://code.google.com/p/android/issues/detail?id=9904#c7
Showcaseshttps://bitbucket.org/informatic0re/awesome-font-iconview
String FontAwesomehttps://gist.github.com/CreatorB/3a934dc4f84d2d887bbd30fc86d1eaa3

No comments :

Post a Comment