5

I am trying to load font icon using fontawesome.ttf in a textView. If i set the string value like  through string.xml then it works file and Icon is shown. But if the same text "" is set programatically as textView.setText("") then it doesn't show the icon , rather the same text is shown on textview.

Any alternative approach to show icons programatically without referring string.xml file ?

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
Atmaram
  • 3,655
  • 2
  • 27
  • 33

4 Answers4

12

Try using the Unicode code... \uf007.
This should work just fine:

textView.setText("\uf007");
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • HI @DerGolem , if i hardcode the string to the textview like textView.setText("\uf007"); then it works fine but the same string if i get it from server and set it to textview then it just shows "\uf007". Any idea if i have to convert / encode the string to set in textview ? – Atmaram Jan 10 '15 at 10:06
  • 1
    `if i get it from server` ptobably, the server uses a different encoding from **Unicode UTF-8**... Try passing (from server) something like `[user-icon]`, then client-side, replace it with `uf007` – Phantômaxx Jan 10 '15 at 19:09
  • @FrankN.Stein i am not able to set icon in textview, try both string from server like  and [] and set to text but it set only  not icon. can you please elaborate? – RaRa Nov 21 '15 at 09:01
  • Simply replace " " with "\uf007". – Phantômaxx Nov 21 '15 at 09:27
6

I tried converting the string with base 16 integer and it worked fine.

new String(Character.toChars(Integer.parseInt(
                        str, 16)))

str has the unicode value like f007.

Atmaram
  • 3,655
  • 2
  • 27
  • 33
2

This is the good example I found so far.

ORIGINAL ANSWER

copy & paste from the answer:

  1. Copied fontawesome-webfont.ttf into your assests folder
  2. Found the character entities for icons I wanted, using this page: http://fortawesome.github.io/Font-Awesome/cheatsheet/
  3. Created an entry in strings.xml for each icon. Eg for a heart:

    <string name="icon_heart">&#xf004;</string>

  4. Referenced said entry in the view of your xml layout:

<Button android:id="@+id/like" ... android:text="@string/icon_heart" />

  1. Loaded the font in my onCreate method and set it for the appropriate Views:

Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" ); Button button = (Button)findViewById( R.id.like ); button.setTypeface(font);

Don't forgot to use setTypeface()

Community
  • 1
  • 1
Shoshi
  • 2,254
  • 1
  • 29
  • 43
0

Put the code in xml and then use

getString(R.string.fa_star_o);

Work fine