I have Spannable object which I want to set its font by a custom font I have loaded before.
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/font_Name.ttf");
Spannable span1 = /*Spannable Item*/;
/// I want to set span1 to have tf font face???
/// Here where I want help.
EDIT :
My problem is that I want to set two different custom fonts for the text view so I am working with the Spannable
Create a CustomTypefaceSpan class:
Use in the same way as the Android framework spans classes:
This answer is based on Imran Rana's answer but does not extend
TypefaceSpan
and then disable its functionality.CustomTypefaceSpan
extendsMetricAffectingSpan
directly.This answer shares a defect with Imran Rana's answer. The span is not parcelled. I.e if you do this (kotlin):
Any
CustomTypefaceSpan
objects set onspannableStringBuilder
will not be marshalled and unmarshalled.If you are using Roboto, you can set a different TypefaceSpan in the constructor
TypefaceSpan typefaceSpan = new TypefaceSpan("sans-serif-medium");
textView.setSpan(typefaceSpan, indexStart, textLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
We don't need to use CustomTypefaceSpan. Here is the solution.
This is a late answer but will help others to solve the issue.
Use the following code:(I'm using Bangla and Tamil font)
The outcome is:
CustomTypefaceSpan Class:
Reference
Try to set your
Spannable
to yourTextView
first and then, try to assign the Typeface to yourTextView
withmyTextView.setTypeface(tf)
;Here is an example where, str is your full string and boldString is the part you need to make bold.