Background
The support library (docs here) allows you to use TTF fonts files in the "res/font" folder, either in XML :
app:fontFamily="@font/lato_black"
or via code:
val typeface = ResourcesCompat.getFont(context, R.font.lato_black)
The problem
While I know it's possible to use spannable technique to set different styles in parts of the TextView content (such as bold, italic, colors,etc...) , the only thing I've found for setting a different font, is by using the built in fonts of the OS, as shown here, but I can't see a way to do it for the new way to load fonts.
What I've tried
I tried to find a way to convert between the two, but with no luck. Of course, I also tried to find in the docs of possible functions, and I've tried to find about it over the Internet.
The question
How to set different font for different parts of the TextView ?
For example, in the text "Hello world", set the "Hello" to have the font of "lato_black" , and the default for the rest.
EDIT: Since I got about the same answer from 2 different people, I can't accept one over the other. Gave them both +1 for the effort, and I've changed the question a bit:
How would I set the font style to a part of the text, easily, while having the strings.xml file define it using a customized font-tag.
For example, this could be in the strings.xml file to set it as I've asked above :
<string name="something" ><customFont fontResource="lato_black">Hello</customFont> world</string>
Then, in code, all you would do is use something like:
textView.setText (Html.fromHtml(text, null, CustomFontTagHandler()))
I think it's very important, because translated strings might have become too different from what's in English, so you can't just parse the text of the string and then choose where to set the custom font. It has to be inside the strings file.
See Question&Answers more detail:
os