package com.tutosandroidfrance.customfont; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Typeface; import android.util.AttributeSet; import android.util.Log; import android.widget.TextView; /** * Created by florentchampigny on 30/06/15. */ public class CustomTextView extends TextView { 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.CustomTextView); String fontName = styledAttrs.getString(R.styleable.CustomTextView_font); styledAttrs.recycle(); setTypeFace(fontName); } public void setTypeFace(String fontName) { if(fontName != null){ try { Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName); setTypeface(typeface); } catch (Exception e) { Log.e("FONT", fontName + " not found", e); } } } }