You need to place your font file in assets folder.
You need to add Typeface to your textview or any other view like below:
This method is suitable for one or few views but for multiple view it will be duplicating the code.
Typeface type = Typeface.createFromAsset(getAssets(),"Kokila.ttf");
txtyour.setTypeface(type);
A better approach is to use your own font manager like this one:
public class QuickFontManager {
private static int cacheSize=6;
private static boolean debuggable=false;
private static LruCache<String, Typeface> lruCache;
private QuickFontManager(int cacheSize, boolean debuggable)
{
QuickFontManager.debuggable=debuggable;
if(lruCache==null)
{
QuickFontManager.cacheSize=cacheSize;
lruCache= new LruCache<String, Typeface>(cacheSize);
}else
{
Log.e("QuickFonts","Cache already configured, use configuration before using typeface. Application class is a good contender.");
}
}
public static void clearCache()
{
if(lruCache!=null)
{
lruCache.evictAll();
lruCache=null;
}
}
/**
*
* @param context
* @param name
* @return A pair containing required typeface and boolean value for whether it was fetched from cache.Boolean works only for debuggable mode.
*/
public static Pair<Typeface, Boolean> getTypeface(Context context, String name) {
if(lruCache==null)
{
lruCache= new LruCache<String, Typeface>(cacheSize);
}
Typeface typeface = lruCache.get(name);
boolean fromCache=true;
if (typeface == null) {
try {
typeface = Typeface.createFromAsset(context.getApplicationContext().getAssets(), name);
fromCache=false;
} catch (Exception e) {
typeface=null;
}
if (typeface == null) {
throw new NullPointerException("Resource named " + name + " not found in assets");
} else
{
lruCache.put(name, typeface);
}
}
if(!QuickFontManager.debuggable)
{
fromCache=true; // User has not asked for debugging ,let's fool views
}
return Pair.create(typeface,fromCache);
}
public static class QuickFontBuilder
{
private int cacheSize;
private boolean debuggable=false;
public QuickFontBuilder()
{
}
public QuickFontBuilder setCachesize(int cachesize)
{
this.cacheSize=cachesize;
return this;
}
public QuickFontManager build()
{
return new QuickFontManager(this.cacheSize,this.debuggable);
}
public QuickFontBuilder setDebuggable(boolean debuggable) {
this.debuggable = debuggable;
return this;
}
}
}
Then create your custom textview or any other view (radio button,checkbox etc) like:
public class TextView extends android.widget.TextView {
private String quickfont;
public TextView(Context context) {
super(context);
init(null, 0);
}
public TextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public TextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
// Load attributes
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextView, defStyle, 0);
try {
quickfont = a.getString(R.styleable.TextView_quickfont);
} catch (Exception e) {
e.printStackTrace();
}finally {
a.recycle();
}
if(quickfont!=null&!isInEditMode())
{
Pair<Typeface,Boolean> pair= QuickFontManager.getTypeface(getContext(), quickfont);
Typeface typeface=pair.first;
boolean fromCache=pair.second;
if(typeface!=null)
{
setTypeface(typeface);
}
if(!fromCache)setTextColor(Color.RED);
}
// Note: This flag is required for proper typeface rendering
setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
and in your values folder create a resource file attrs.xml(if not created) and add this:
<!--for custom views-->
<declare-styleable name="TextView">
<attr name="quickfont" format="string" />
</declare-styleable>
all the hard work is done.Now you can simple use it in your xml for any view like
<com.abc.views.TextView
app:quickfont="OpenSans-Regular_1.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
/>
see that i assigned the font using app:quickfont.Where OpenSans-Regular_1.ttf is my font file in assets folder and com.abc.views.TextView is my custom textview.