155

I'm trying to use Font Awesome on my application, I was able to integrate the font using Typeface.createFromAsset(), but I also want to use the icons provided by this font, but so far I haven't been able to do that.

This particular font contains icons inside the Unicode Private Use Area (PUA), for things like media player controls, file system access, arrows, etc.

Has anybody used fonts that contain icons and symbols on Android, is this possible at all?

onexf
  • 3,674
  • 3
  • 22
  • 36
Julian Suarez
  • 4,499
  • 4
  • 24
  • 40
  • 3
    https://github.com/FortAwesome/Font-Awesome – f20k Mar 04 '13 at 20:48
  • 24
    I don't agree that this is off-topic for Stack Overflow. It seems to me that it fits these (http://stackoverflow.com/help/on-topic) guidelines, as it's: about a specific programming problem (how to apply a specific font-based icon set to a specific mobile platform), and it's a practical, answerable problem (see my answer below, which I think illustrates that). – Keith Corwin Jun 17 '13 at 12:46
  • check this library: http://blog.shamanland.com/p/android-fonticon-library.html, it's available on Maven Central. see demo-app: https://play.google.com/store/apps/details?id=com.shamanland.fonticon.example – Oleksii K. Jun 22 '14 at 10:44
  • Check out this repo for implementing font awesome in android https://github.com/bperin/FontAwesomeAndroid – Brian Oct 02 '13 at 05:28
  • After trying this in 2018, I decided Google Material Icons are better fit for Android https://stackoverflow.com/questions/28684759/import-material-design-icons-into-an-android-project – Daniel Viglione Feb 28 '18 at 01:22
  • Check out my blog post (works on latest android) - https://niveditagautam.wordpress.com/2018/10/15/adding-font-awesome-to-your-android-app – user63762453 Jan 03 '19 at 23:31

15 Answers15

311

Font Awesome seems to be working fine for me in my android app. I did the following:

  1. Copied fontawesome-webfont.ttf into my 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 my xml layout:

     <Button
         android:id="@+id/like"
         style="?android:attr/buttonStyleSmall"
         ...
         android:text="@string/icon_heart" />
    
  5. 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);
    
hsz
  • 148,279
  • 62
  • 259
  • 315
Keith Corwin
  • 3,234
  • 1
  • 12
  • 3
  • 47
    Great idea. But beware of memory issues when constantly creating a new typeface for each icon. Instead, use something like `Hashtable` to re-use your icon typeface, like suggested here: https://code.google.com/p/android/issues/detail?id=9904#c7 – saschoar Apr 25 '13 at 09:58
  • 1
    i do it like this: http://stackoverflow.com/questions/2973270/using-a-custom-typeface-in-android/7497949#7497949 – Informatic0re Apr 25 '13 at 10:34
  • 4
    I created an Project for this: https://bitbucket.org/informatic0re/awesome-font-iconview – Informatic0re Jun 20 '13 at 09:00
  • I also implemented simple tool for ```fontastic```, it's able to extract font from archive and create ```strings.xml``` with corresponding values: https://github.com/shamanland/fonticon-prepare/ – Oleksii K. Apr 23 '14 at 21:11
  • May I know please what is the pros of using font awesome in android. – user2273146 Mar 18 '15 at 06:01
  • @user2273146 Similar benefits as for the web. 500+ icons for free in a single small package (122 KB as of v4.3.0). Resolution independent - no worrying about sizing assets for different device densities. You can also apply color and use them anywhere you'd use text (even mixing them in with normal text). If nothing else, it's extremely handy for prototyping. – Keith Corwin Mar 19 '15 at 18:05
  • How would you adjust the size of the icons ? – MrJoshFisher May 06 '15 at 17:47
  • 1
    I have some problems with my code: If I declare the string in `strings.xml` like `` then set text in my code, it will be ok. But when I try `mTextView.setText("");` it only show the raw text. Anyone could helps ? thks – vtproduction Jul 30 '15 at 02:43
  • 1
    This way you set the text to be exactly what you wrote within quotation marks, while you're probably looking for something like `mTextView.setText(getResources().getString(R.string.faicon) )` – Andre Jul 30 '15 at 13:14
  • How to add text as well as icon in button using awesome font? Like add drawable left or drawable right in button. – Siddharth_Vyas Nov 03 '15 at 06:14
  • 1
    i am getting  this code from server as a string and set to textview but it diplay only code not a icon, how to set icon? – RaRa Nov 21 '15 at 08:56
  • To generate these `` tags for all FontAwesome icons, I made a small PHP code that can convert the FontAwesome CSS to XML `` definitions: http://codepad.org/YPXKDomY/raw.php – Philip Mar 04 '16 at 23:01
  • 2
    Add dynamic string to textview like this text.setText(Html.fromHtml("")); – rana_sadam Apr 20 '16 at 10:31
  • 1
    "" is for xml, setText() is java function so, it should be "\uf107". – Chlind Jul 19 '16 at 02:36
  • 1)add .ttf file in assets folder 2)add string entry in string.xml file 3)use textview to display font awesome icon like "text=fa_facebook" but it will display croll icon always what is the problem i don't know please help me thanks in advanced – Ganpat Kaliya Oct 27 '16 at 06:32
  • I prefer this syntax, since it's shorter: `\uf004` – Phantômaxx Nov 18 '16 at 16:21
  • Hi anyone knows how to use `regular` icon or `bold` icons example - in cheet sheet page the code for thumbsup icon is `f164` for both solid and regular but when I use it like this it only shows the bold one not the regular one .. any help with this ? – shadygoneinsane Feb 20 '18 at 06:38
  • Can anyone help me with this https://stackoverflow.com/q/48879484/5134647 – shadygoneinsane Mar 08 '18 at 11:18
  • I used `fa-solid-900.ttf` to be able to add solid icons to my app like add user. if you want to add brand logo like facebook or twitter use `fa-brands-400`. It's V5.0.13 – Sam May 24 '18 at 14:09
  • There are many phones like Honor the icon fonts are not working. On these devices the icon code is printed as it is. Any fix for such devices. – Manmohan Soni Apr 11 '19 at 06:41
  • how use it with item icon menu? – Fernando Torres Oct 02 '19 at 03:45
29

Try IcoMoon: http://icomoon.io

  • Pick the icons you want
  • Assign characters to each icon
  • Download the font

Say, you picked the play icon, assigned the letter 'P' to it, and downloaded the file icomoon.ttf to your asset folder. This is how you show the icon:

xml:

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="48sp"
  android:text="P" />

java:

Typeface typeface = Typeface.createFromAsset(getAssets(), "icomoon.ttf");
textView.setTypeface(typeface);

I've given a talk on making beautiful Android apps, which includes explanation on using icon fonts, plus adding gradients to make the icons even prettier: http://www.sqisland.com/talks/beautiful-android

The icon font explanation starts at slide 34: http://www.sqisland.com/talks/beautiful-android/#34

chiuki
  • 14,580
  • 4
  • 40
  • 38
10

Maybe too late but I had the same need so I've published this https://github.com/liltof/font-awsome-for-android It's an android ready xml version of font awesome usable just like Keith Corwin said

Hope it will help others.

liltof
  • 2,153
  • 2
  • 14
  • 23
  • 1
    Welcome to SO. I saw your code is not too long. Why don't you post it at this answer? Links can crash with time. – Andre Silva May 29 '13 at 18:54
  • I have been using this great .xml file for weeks. But now I've realized that it misses fa_times. Can you please update it ? Edit: oh, it is fa_remove. or icon_remove in your file. – mobilGelistirici Nov 21 '13 at 13:44
  • How to add text as well as icon in button using awesome font? Like add drawable left or drawable right in button. – Siddharth_Vyas Nov 03 '15 at 06:15
9

As above is great example and works great:

Typeface font = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf" );

Button button = (Button)findViewById( R.id.like );
button.setTypeface(font);

BUT! > this will work if string inside button you set from xml:

<string name="icon_heart">&#xf004;</string>
button.setText(getString(R.string.icon_heart));

If you need to add it dynamically can use this:

String iconHeart = "&#xf004;";
String valHexStr = iconHeart.replace("&#x", "").replace(";", "");
long valLong = Long.parseLong(valHexStr,16);
button.setText((char) valLong + "");
user1540907
  • 191
  • 2
  • 5
5

There is small and useful library designed for this purposes:

dependencies {
    compile 'com.shamanland:fonticon:0.1.9'
}

Get demo on Google Play.

enter image description here

You can easily add font-based icon in your layout:

<com.shamanland.fonticon.FontIconView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ic_android"
    android:textSize="@dimen/icon_size"
    android:textColor="@color/icon_color"
    />

You can inflate font-icon as Drawable from xml:

<?xml version="1.0" encoding="utf-8"?>
<font-icon
    xmlns:android="http://schemas.android.com/apk/res-auto"
    android:text="@string/ic_android"
    android:textSize="@dimen/big_icon_size"
    android:textColor="@color/green_170"
    />

Java code:

Drawable icon = FontIconDrawable.inflate(getResources(), R.xml.ic_android);

Links:

Oleksii K.
  • 5,359
  • 6
  • 44
  • 72
4

If you want programmatic setText without add string to string.xml

see its hexadecimal code here:

http://fortawesome.github.io/Font-Awesome/cheatsheet/

replace &#xf066; to 0xf066

 Typeface typeface = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf");
    textView.setTypeface(typeface);
    textView.setText(new String(new char[]{0xf006 }));
Haryanto
  • 609
  • 9
  • 17
2

One of the libraries that I use for Font Awesome is this:

https://github.com/Bearded-Hen/Android-Bootstrap

Specifically,

https://github.com/Bearded-Hen/Android-Bootstrap/wiki/Font-Awesome-Text

The documentation is easy to understand.

First, add the needed dependencies in the build.gradle:

dependencies {
    compile 'com.beardedhen:androidbootstrap:1.2.3'
}

Secondly, you can add this in your XML:

<com.beardedhen.androidbootstrap.FontAwesomeText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    fontawesometext:fa_icon="fa-github"
    android:layout_margin="10dp" 
    android:textSize="32sp"
/>

but make sure you add this in your root layout if you want to use above code example:

    xmlns:fontawesometext="http://schemas.android.com/apk/res-auto"
Abdul Rahman A Samad
  • 1,062
  • 1
  • 15
  • 21
2

I made this helper class in C# (Xamarin) to programmatically set the text property. It which works pretty well for me:

internal static class FontAwesomeManager
{
    private static readonly Typeface AwesomeFont = Typeface.CreateFromAsset(App.Application.Context.Assets, "FontAwesome.ttf");

    private static readonly Dictionary<FontAwesomeIcon, string> IconMap = new Dictionary<FontAwesomeIcon, string>
    {
        {FontAwesomeIcon.Bars, "\uf0c9"},
        {FontAwesomeIcon.Calendar, "\uf073"},
        {FontAwesomeIcon.Child, "\uf1ae"},
        {FontAwesomeIcon.Cog, "\uf013"},
        {FontAwesomeIcon.Eye, "\uf06e"},
        {FontAwesomeIcon.Filter, "\uf0b0"},
        {FontAwesomeIcon.Link, "\uf0c1"},
        {FontAwesomeIcon.ListOrderedList, "\uf0cb"},
        {FontAwesomeIcon.PencilSquareOutline, "\uf044"},
        {FontAwesomeIcon.Picture, "\uf03e"},
        {FontAwesomeIcon.PlayCircleOutline, "\uf01d"},
        {FontAwesomeIcon.SignOut, "\uf08b"},
        {FontAwesomeIcon.Sliders, "\uf1de"}
    };

    public static void Awesomify(this TextView view, FontAwesomeIcon icon)
    {
        var iconString = IconMap[icon];

        view.Text = iconString;
        view.SetTypeface(AwesomeFont, TypefaceStyle.Normal);
    }
}

enum FontAwesomeIcon
{
    Bars,
    Calendar,
    Child,
    Cog,
    Eye,
    Filter,
    Link,
    ListOrderedList,
    PencilSquareOutline,
    Picture,
    PlayCircleOutline,
    SignOut,
    Sliders
}

Should be easy enough to convert to Java, I think. Hope it helps someone!

1

The FontView library lets you use normal/unicode font characters as icons/graphics in your app. It can load the font via assets or a network location.

The benefit of this library is that:

1 - it takes care of remote resources for you
2 - scales the font size in dynamically sized views
3 - allows the font to easily be styled.

https://github.com/shellum/fontView

Example:

Layout:

<com.finalhack.fontview.FontView
        android:id="@+id/someActionIcon"
        android:layout_width="80dp"
        android:layout_height="80dp" />

Java:

fontView.setupFont("fonts/font.ttf", character, FontView.ImageType.CIRCLE);
fontView.addForegroundColor(Color.RED);
fontView.addBackgroundColor(Color.WHITE);
David Passmore
  • 6,089
  • 4
  • 46
  • 70
Shellum
  • 3,159
  • 2
  • 21
  • 26
  • Does it cache the Typeface object in memory? There's a known potential for a memory leak when handling fonts on Android. – TheRealChx101 Sep 14 '18 at 20:54
1

There's another nice solution which you can use in your layout xml files directly and does not require to use setTypeface.

It is Joan Zapata's Iconify. You can read here what's new in Iconify v2. It includes 9 different font libraries which you can simply use by adding dependencies to your build.gradle file.

In the layout xml files it's possible to choose between these widgets:

com.joanzapata.iconify.widget.IconTextview
com.joanzapata.iconify.widget.IconButton
com.joanzapata.iconify.widget.IconToggleButton
gus27
  • 2,616
  • 1
  • 21
  • 25
1

Initially create asset folder and copy the fontawesome icon (.ttf) How to create asset folder?

app-->right Click -->new-->folder --> asset folder

next step download how to download .ttf file? click here--> and click download button after download extract and open web fonts. finally choose true text style(ttf)paste asset folder.

how to design xml and java file in android ?

app-->res-->values string.xml

resources
    string name="calander_font" >&#xf073; <string
resources

this example of one font more Unicode click here

Activity_main.xml

 <TextView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/calander_view"/>

MainActivity.java

calander_tv = (TextView)findViewById(R.id.calander_view);

Typeface typeface = Typeface.createFromAsset(getAssets(),"/fonts/fa-solid-900.ttf");
calander_tv.setTypeface(typeface);
calander_tv.setText(R.string.calander_font);

Output:

Output image

Rishabh Agarwal
  • 1,988
  • 1
  • 16
  • 33
0

I'm a bit late to the party but I wrote a custom view that let's you do this, by default it's set to entypo, but you can modify it to use any iconfont: check it out here: github.com/MarsVard/IconView

// edit the library is old and not supported anymore... new one here https://github.com/MarsVard/IonIconView

Mars
  • 4,197
  • 11
  • 39
  • 63
  • Your library is not caching the Typeface and, instead, is constructing it every time the view is rendered. – Fernando Camargo Feb 04 '14 at 00:24
  • 1
    @FernandoCamargo is Right, this library is old and should be updated with better caching... here is the new library with better performance https://github.com/MarsVard/IonIconView – Mars Feb 04 '14 at 09:21
0

In case you only need a few font awesome icons, you can also use http://fa2png.io to generate normal pixel images. But if you add new icons/buttons regularly I'd recommend the .ttf version as its more flexible.

j7nn7k
  • 17,995
  • 19
  • 78
  • 88
0

If someone wonders how to add it programmitcally you gotta do it this way.

   button_info.setText(R.string.icon_heart);
    button_info.append(" Hallo"); //<<--- This is the tricky part
Tower Jimmy
  • 567
  • 1
  • 4
  • 15
0

As all answers are great but I didn't want to use a library and each solution with just one line java code made my Activities and Fragments very messy. So I over wrote the TextView class as follows:

public class FontAwesomeTextView extends TextView {
private static final String TAG = "TextViewFontAwesome";
public FontAwesomeTextView(Context context) {
    super(context);
    init();
}

public FontAwesomeTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public FontAwesomeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public FontAwesomeTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
}

private void setCustomFont(Context ctx, AttributeSet attrs) {
    TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
    String customFont = a.getString(R.styleable.TextViewPlus_customFont);
    setCustomFont(ctx, customFont);
    a.recycle();
}

private void init() {
    if (!isInEditMode()) {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fontawesome-webfont.ttf");
        setTypeface(tf);
    }
}

public boolean setCustomFont(Context ctx, String asset) {
    Typeface typeface = null;
    try {
        typeface = Typeface.createFromAsset(ctx.getAssets(), asset);
    } catch (Exception e) {
        Log.e(TAG, "Unable to load typeface: "+e.getMessage());
        return false;
    }

    setTypeface(typeface);
    return true;
}
}

what you should do is copy the font ttf file into assets folder .And use this cheat sheet for finding each icons string.

hope this helps.

Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36