Android Custom font Spannable Typeface Span Android Custom font Spannable Typeface Span android android

Android Custom font Spannable Typeface Span


You are going to want to write a custom TypefaceSpan. The good news is that they aren't difficult. See here for an example.


You can do the following:

SpannableString spannableString = new SpannableString("your text");Typeface typeface = Typeface.create(ResourcesCompat.getFont(this, R.font.your_font);spannableString.setSpan(    new TypefaceSpan(typeface, Typeface.NORMAL)),     0, 4,     Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

This constructor will replace the previous style of the text, achieving what you want. However, the catch is that it was introduced in API 28.