TheJadav

Use font-awesome for Icon in android

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))));

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message