Saturday, January 4, 2014

Using custom fonts in Android

In Android their is support for adding custom fonts using Typeface in java file.

But it would be more helpful if Typeface can set in XML file just like setting default android fonts android:typeface="sans"

So first of all i assigned all the fonts prefixed with numbers


Then have a look at this code snippet

<!-- This code need to be pasted in res --- value -- attrs.xml file -->
<resources>
<declare-styleable name="TypefacedTextView">
<attr name="typeface" format="string" /> <!-- enter integer values -->
</declare-styleable>
</resources>
package com.test.customWidgets;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextView extends TextView{
Utility utility = new Utility();
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
//Typeface.createFromAsset doesn't work in the layout editor. Skipping...
if (isInEditMode()) {
return;
}
TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView);
String fontNos = styledAttrs.getString(R.styleable.TypefacedTextView_typeface);
styledAttrs.recycle();
String font = utility.getFontName(Integer.valueOf(fontNos));
if(font != null) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/"+font);
setTypeface(typeface);
}
}
}
//Programatically setting font. This is tedious i condiser , instead used UsingInLayoutFille.xml.
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/10TitilliumWeb-SemiBold.ttf");
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(myTypeface);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.test.map" //This is important , this will connect custom:typeface=""
android:id="@+id/relative_main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.test.customWidgets.CustomTextView
android:id="@+id/privacy_text"
style="@style/login_terms_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="privacyOnClick"
android:textColor="#646464"
custom:typeface="10"//setting 10TitilliumWeb-SemiBold.ttf font
android:text="@string/login_privacy_policy" />
</RelativeLayout>
package com.test.customWidgets;
import android.widget.EditText;
public class Utility {
private final String TW_SEMI_BOLD = "10TitilliumWeb-SemiBold.ttf";
private final String TW_SEMI_BOLD_ITALIC = "11TitilliumWeb-SemiBoldItalic.ttf";
private final String TW_BLACK = "1TitilliumWeb-Black.ttf";
private final String TW_BOLD = "2TitilliumWeb-Bold.ttf";
private final String TW_BOLD_ITALIC = "3TitilliumWeb-BoldItalic.ttf";
private final String TW_EXTRA_LIGHT = "4TitilliumWeb-ExtraLight.ttf";
private final String TW_EXTRA_LIGHT_ITALIC = "5TitilliumWeb-ExtraLightItalic.ttf";
private final String TW_ITALIC = "6TitilliumWeb-Italic.ttf";
private final String TW_LIGHT = "7TitilliumWeb-Light.ttf";
private final String TW_LIGHT_ITALIC = "8TitilliumWeb-LightItalic.ttf";
private final String TW_REGULAR = "9TitilliumWeb-Regular.ttf";
public String getFontName(int fontNos) {
String font = null;
switch (Integer.valueOf(fontNos)) {
case 1:font = TW_BLACK;break;
case 2:font = TW_BOLD;break;
case 3:font = TW_BOLD_ITALIC;break;
case 4:font = TW_EXTRA_LIGHT;break;
case 5:font = TW_EXTRA_LIGHT_ITALIC;break;
case 6:font = TW_ITALIC;break;
case 7:font = TW_LIGHT;break;
case 8:font = TW_LIGHT_ITALIC;break;
case 9:font = TW_REGULAR;break;
case 10:font = TW_SEMI_BOLD;break;
case 11:font = TW_SEMI_BOLD_ITALIC;break;
default:
break;
}
return font;
}
}
view raw Utility.java hosted with ❤ by GitHub