How to use Font Awesome icon in android application?
You can also import the raw vector images from Font Awesome and import them as drawables in your project with the help of creating a new Vector Asset in Android Studio:
Go to the folder where you have installed Font Awesome
...../fontawesome-pro-version#/advanced-options/raw-svg
There you will find 4 folders: brands, light, regular and solid.
All the icons are available as separated vector images in those 4 folders
To import an icon, go to resources, right click and select New -> Vector asset. A dialog will open. Select Local file as option, and select the vector image you want to import (path). Name of the image will be substracted from the image file.
And then you can resolve your problem by simple using a compound TextView, something like this:
<TextView
android:drawableStart="@drawable/my_imported_fontawesome_login_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login Now"
android:gravity="center_vertical"/>
One advantage of this solution is, that you only need to import the icons you really need, without the overhead of a complete or even more fonts, as the icons are divided over 4 fonts.
Here are the steps to follow:
- First download font-awesome from here: http://fontawesome.io/
- Create assets folder and add fontawesome-webfont.ttf to this folder
- Create a helper class by below code:
public class FontManager {
public static final String ROOT = "fonts/",
FONTAWESOME = ROOT + "fontawesome-webfont.ttf";
public static Typeface getTypeface(Context context, String font) {
return Typeface.createFromAsset(context.getAssets(), font);
}
}
4. Now use font awesome to your textview using below code
Typeface iconFont = FontManager.getTypeface(getApplicationContext(), FontManager.FONTAWESOME);
tvIcon1 = (TextView) findViewById(R.id.tvIcon1);
tvIcon2 = (TextView) findViewById(R.id.tvIcon2);
tvIcon3 = (TextView) findViewById(R.id.tvIcon3);
tvIcon1.setTypeface(iconFont);
tvIcon2.setTypeface(iconFont);
tvIcon3.setTypeface(iconFont);
You can get full source code in my blog post here.
You can follow this answer.
First Download the fontawesome.ttf from here. And put the file in asset/fontawesome.ttf.
Then Make a FontAwesome
class which actually represents the textview of FontAwesome
like this way.
public class FontAwesome extends TextView {
public FontAwesome(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public FontAwesome(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontAwesome(Context context) {
super(context);
init();
}
private void init() {
//Font name should not contain "/".
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fontawesome.ttf");
setTypeface(tf);
}
}
now you can use the Fontawesome class as your need and also follow the cheatsheet. to get your icon's Unicode.
So, your TextView will be like this.
<PACKAGE_NAME.FontAwesome
android:id="@+id/userLogin"
android:text=" Login Now"
android:clickable="true"
android:onClick="login"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />