August 13, 2021

Use font-awesome for Icon in android

By lj007

Use font-awesome for Icon in android

Here is steps to how to use font-awesome fonts in Android :

  • Download font-awesome from here
  • Create assets folder in app/src/main directory.
  • Now create fonts folder in assets folder
  • You can use it in two ways… either create Custom class which extends TextView or Manually get instance of TextView in Activity and then set font.
  • To choose font visit this site and check all icons.
  • Here describes both ways..  

Create Helper class :

public class CustomFontFamily
{
    static CustomFontFamily customFontFamily;
    HashMap fontMap=new HashMap<>();
    public static CustomFontFamily getInstance()
    {
        if(customFontFamily==null)
            customFontFamily=new CustomFontFamily();
        return customFontFamily;
    }
    public void addFont(String alias, String fontName){
        fontMap.put(alias,fontName);
    }
    public Typeface getFont(String alias)
    {
        String fontFilename = fontMap.get(alias);
        if (fontFilename == null) {
            Log.e("", "Font not available with name " + alias);
            return null;
        }
        else
        {
            Typeface typeface = Typeface.createFromAsset(CustomApplication.getContext().getAssets(), "fonts/" + fontFilename);
            return typeface;
        }
    }
}

Create Custom TextView :

public class IconTextView extends android.support.v7.widget.AppCompatTextView {

public IconTextView(Context context) { super(context); setFont(); }

public void setFont() { setTypeface(CustomFontFamily.getInstance().getFont(“fontawesome-webfont”)); }

public IconTextView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

setFont();

}

public IconTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr); setFont();

}

}  

Use in XML: 
            android:layout_width=”38dp”
            android:layout_height=”38dp”
            android:layout_alignParentLeft=”true”
            android:layout_gravity=”right”
            android:layout_marginBottom=”5dp”
            android:layout_marginLeft=”5dp”
            android:layout_marginRight=”5dp”
            android:layout_marginTop=”5dp”
            android:layout_weight=”1″
            android:gravity=”center”
            android:text=””
            android:textAllCaps=”false”
            android:textColor=”@color/white”
            android:textSize=”24sp”
            android:visibility=”gone” />


 Or just create Normal TextView in XML:

            android:layout_width=”38dp”
            android:layout_height=”38dp”
            android:layout_alignParentLeft=”true”
            android:layout_gravity=”right”
            android:layout_marginBottom=”5dp”
            android:layout_marginLeft=”5dp”
            android:layout_marginRight=”5dp”
            android:layout_marginTop=”5dp”
            android:layout_weight=”1″
            android:gravity=”center”
            android:text=””
            android:textAllCaps=”false”
            android:textColor=”@color/white”
            android:textSize=”24sp”
            android:visibility=”gone” />

And use it in Activity :

Typeface fontAwesome = CustomFontFamily.getInstance().getFont("fontawesome-webfont");
TextView tvIcon = (TextView) orderView.findViewById(R.id.total_icon);
tvIcon.setTypeface(fontAwesome);

Dynamically set font by using its hax value : 

tvIcon.setText(new String(Character.toChars(Integer.parseInt("F00D", 16))));