how to set CustomFont full application?

January 07, 2016 , , 0 Comments

The first step is to add a attrs.xml in your /values/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomFontTextView">
<attr name="font" format="string"/>
</declare-styleable>
</resources>

Second Step : String resource in /values/strings.xml
<resources>
<string name="opensans_regular">opensanRegular</string>
<string name="opensans_bold">opensansBold</string>
</resources>

Add CustomFont class: (Here i used OpenSansFont )
 public class OpenSansTextView extends TextView {

public OpenSansTextView(Context context) {
super(context);
CustomFontUtils.applyCustomFont(this, context, null);
}

public OpenSansTextView(Context context, AttributeSet attrs) {
super(context, attrs);
CustomFontUtils.applyCustomFont(this, context, attrs);
}

public OpenSansTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
CustomFontUtils.applyCustomFont(this, context, attrs);
}
}

Uses the CustomFontUtils class :
public class CustomFontUtils {

public static final String ANDROID_SCHEMA = "http://schemas.android.com/apk/res/android";

public static void applyCustomFont(TextView customFontTextView, Context context, AttributeSet attrs) {
TypedArray attributeArray = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView);

String fontName = attributeArray.getString(R.styleable.CustomFontTextView_font);
int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", 0);

Typeface customFont = selectTypeface(context, fontName, textStyle);
customFontTextView.setTypeface(customFont);

attributeArray.recycle();
}

private static Typeface selectTypeface(Context context, String fontName, int textStyle) {
if (fontName.contentEquals(context.getString(R.string.opensans))) {
switch (textStyle) {
case 1: // bold
return FontCache.getTypeface("OpenSansBold.ttf", context);
case 2: // italic
// return FontCache.getTypeface("Roboto-Black.ttf", context);
case 0: // regular
return FontCache.getTypeface("OpenSansRegular.ttf", context);

default:
return FontCache.getTypeface("OpenSansRegular.ttf", context);
}
}
//if more than one font use in single application at that time add more else if
/*else if (fontName.contentEquals(context.getString(R.string.roboto_thin))) {
switch (textStyle) {
case 1: // bold
return FontCache.getTypeface("Roboto-Thin.ttf", context);
case 2: // italic
return FontCache.getTypeface("Roboto-Thin.ttf", context);
case 0: // regular
default:
return FontCache.getTypeface("Roboto-Thin.ttf", context);
}*/

else {
return null;
}
}

}

 

Here Uses FontCach for this :
public class FontCache {
private static Hashtable fontCache = new Hashtable();

public static Typeface getTypeface(String fontname, Context context) {
Typeface typeface = fontCache.get(fontname);

if (typeface == null) {
try {
typeface = Typeface.createFromAsset(context.getAssets(), fontname);
} catch (Exception e) {
return null;
}

fontCache.put(fontname, typeface);
}

return typeface;
}
}

Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Popular Posts