How To Use Font Awesome in Android Studio

Android studio by default gives very fewer fonts/icons which you can access through creating Image Assets or Vector Assets. so here I am going to tell you how to add ‘font awesome’ in your android studio project.

You can place Font Awesome icons just about anywhere using a style prefix and the icon’s name. They have created icons which will take on the characteristics and appear alongside text naturally.

To Know what are font awesome check my previous post with more tools to get help while designing an application here.

so let’s begin

*please keep the folders name same as I did.

you can also go to app> new > module > Assets to add assets folder to your android studio. Then you can paste the .ttf file to the asset folder.

Create A new Android Studio Project > MyFontAwesomeExample.

Create A new Folder Anywhere on your pc named assets> make a new folder in assets > fonts> in fonts paste the font awesome ttf file. Download font awesome.ttf.

Now go to project drop down and go to the main folder and paste assets folder in it.

create a new java class MyTextViewFontAwesome.java


import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyTextViewFontAwesome extends TextView {
public MyTextViewFontAwesome(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextViewFontAwesome(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextViewFontAwesome(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
 "fonts/fontawesome.ttf");
setTypeface(tf);
}
}
}

now go to values resource file and create new resource file called icon.xml

     digit code for icon;
    * watch my video to know where to get the code for icon!

Go to Values folder and create a new Icon File (Select type of file icon). Now Write in resource tag:

<item name="myfontawesome">&#xfour digit code;</item>

now go to layout resource file

    <com.infy.fontawesome.MyTextViewFontAwesome
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/icon_cash"
        android:textSize="200sp"
        android:textColor="@color/colorPrimaryDark"/>



Now run the app, you can add any 4 digits font awesome text in your 
application now.To know more please watch my video.

Tips:

  1. &#xf456; is the format of using font awesome. You just need to change the four character+digit to change your icon.
  2. Create an icon file in your values folder to add font awesome shortcodes.
  3. use naming convention correctly. Name the icon where you are going to use it like as a password icon. user icon, lock icon, logo etc.
  4. To use font awesome always use  <com.i.fontawesome.MyTextViewFontAwesome/> this will not work with simple<textview/> tag.
  5. Font Awesome is not By Default clickable so mention clickable=true if you want to add click listeners to font awesome icon.
  6. There are some icons which are only available to Premium User/Paid users. If you are using a free account you can not access those free icons even if you get the four digit_character code.

[amazon_link asins=’B07957FC7N,B07BTZC1CG,B01HPOUUJO,B072C5VYTN,B079VHVJJ4,B076YLTPVL,B01HPOVSEA|B0046Z23P0,B00J4TE7N4,B00YXUE5TU,B0751GSFBC,B074WXV9LV,B00CSMA7H4|B01L3TCZ22,B01H2SN306,B07HYG92DQ,B00KGC06O2,B009F0N68E,B0146KGQNY|B00EOC6PYA,B00JP4YBUC,B01L3TCZ22,B06Y422QMM,B07FD425LF,B00KGC06O2′ template=’ProductCarousel’ store=’lifenews525-21|lifenews-21|lifenews06-21|lifenews0b-21′ marketplace=’IN|UK|FR|IT’ link_id=’ c2960f98-d6bc-11e8-9a9c-99987cf1e608′]

Advertisements

3 comments

  1. […] 2. Font Awesome library  Well this has a free version and a paid version, most of your work will be done within the free version only. you just have to search the icon by its name or any clue you have about it. This website will give you all the icons related to the word and display both paid and free version. you must know that it also reduced the size of the application as, its just a text, not an image. To know how to use font awesome library in your android application click here. […]

Your Views And Question are Welcome Here!