Custom TextView Java Class
Put this class in com.util package
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextViewRegular extends TextView {
public CustomTextViewRegular(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public CustomTextViewRegular(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CustomTextViewRegular(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs != null) {
// Set type face
Typeface myTypeface = Typeface.createFromAsset(getContext()
.getAssets(), "Helvetica_Neue.ttf");
setTypeface(myTypeface);
}
}
}
You must have Helvetica_Neue.ttf in assets folder, you can use other font as well.
How to use in xml
<com.util.CustomTextViewRegular
android:id="@+id/txtCustom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#727272"
android:textSize="20dip" />
If you want to change font than you have to just change font type in Java class only.
Done