If you are setting the text to the TextView by the method
textView.setText(Html.fromHtml(text));
The html styling of the text (even if is empty might override the previous font you set on the TextView. To fix it you could override the method setText
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
if (fontType != null) {
initTypeface();
}
}
My complete solution so you can use custom TextView on code and on xml is (assuming that you have the font files under assets/font/ folder
public class CustomTextView extends TextView {
String TAG = CustomTextView.class.getSimpleName();
public enum FontType {
MONTSERRAT_BOLD, MONSERRAT_REGULAR, PROXIMANOVA_REGULAR, PROXIMANOVA_REGULAR_ITALIC
}
/* Default font */
private FontType fontType = FontType.MONSERRAT_REGULAR;
/* Constructor from xml */
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initTypeface(attrs);
}
/* Constructor from xml */
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initTypeface(attrs);
}
/* Constructor from code */
public CustomTextView(Context context) {
super(context);
initTypeface();
}
/* Constructor from code */
public CustomTextView(Context context, FontType fontType) {
super(context);
this.fontType = fontType;
initTypeface();
}
private void initTypeface() {
setLineSpacing(10, 1);
String fontName = null;
switch (fontType) {
case MONTSERRAT_BOLD:
fontName = "MontserratBold.otf";
break;
case MONSERRAT_REGULAR:
fontName = "MontserratRegular.otf";
break;
case PROXIMANOVA_REGULAR:
fontName = "ProximaNovaRegular.otf";
break;
case PROXIMANOVA_REGULAR_ITALIC:
fontName = "ProximaNovaRegularItalic.otf";
break;
default:
fontName = "MontserratRegular.otf";
}
if (!TextUtils.isEmpty(fontName)) {
Typeface t = Typeface.createFromAsset(getContext().getAssets(), "font/" + fontName);
this.setTypeface(t);
}
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
if (fontType != null) {
initTypeface();
}
}
private void initTypeface(AttributeSet attrs) {
TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTextView, 0, 0);
if (a != null) {
int enumPosition = a.getInteger(R.styleable.CustomTextView_fontType, 0);
this.fontType = FontType.values()[enumPosition];
initTypeface();
}
}
}
To be able to instantiate the class on xml you need to create an enum under values attrs.xml
<declare-styleable name="CustomTextView">
<attr name="fontType" format="enum">
<enum name="monserrat_bold" value="0" />
<enum name="monserrat_regular" value="1" />
<enum name="proximanova_regular" value="2" />
<enum name="proximanova_italic" value="3" />
</attr>
</declare-styleable>
Then you can instantiate your view on xml
<com.yourpackagename.views.textViews.CustomTextView xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/test_name_text_view"
custom:fontType="monserrat_bold"
style="@style/AppFont.Assessment.TestTitle"
android:layout_marginStart="@dimen/top_bar_margin_start"
android:layout_marginTop="@dimen/top_bar_margin_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
or from the code
CustomTextView textView = new CustomTextView(context, CustomTextView.FontType.MONSERRAT_REGULAR);