Muddz/StyleableToast

Fonts in Xml styles not working with API 26 xml Fonts

Closed this issue · 7 comments

when using style.xml, font attribute does't work with api 26 font xml font system.

<style name="toastStyle">
    <item name="textFont">@font/inconsolata</item>
</style>

i'm still able to change it in code using .typeface(ResourcesCompat.getFont(context, R.font.inconsolata)) with the builder.
am i missing something?

Muddz commented

Hi! First of all your fonts must be in the assets/fonts folder and then just type
<item name="textFont">fonts/inconsolata.ttf</item>
remember the correct font extension .ttf or .otf

This is to support backward compatibility down to API 16

I will make an update later to support the @font/ way

The support library 26 support xml fonts in API 14 and later.
U could use ResourcesCompat.getFont(context, R.font.myfont) instead of Typeface.createFromAsset(context.getAssets(), textFontPath)

I will use the builder for now. Thanks!

Muddz commented

Thanks for the suggestion! I will look into it as soon as possible

Muddz commented

@Stevenmagdy This is now added in 2.0.1 both from code .font(R.font.xxx) or in via xml <item name="textFont">@font/dosis</item>

To make it more simple you dont have to use getFont() or ResourceCompat.getFont() but just the font resourceId

I tried and it doesn't apply the font with bold text. you should make sure after you fix this that the system uses the bold weight not just increase the font stroke.
when the font family is :

<font-family>
<font
    app:font="@font/a_regular"
    app:fontStyle="normal"
    app:fontWeight="400"/>
<font
    app:font="@font/a_bold"
    app:fontStyle="normal"
    app:fontWeight="700"/>
</font-family>

and you set the font to bold it should use a_bold.ttf not increase the stroke of a_regular.ttf, this should be fixed with using AppCompatTextView instead of TextView. it's a small detail but makes a difference in some fonts.

Muddz commented

@Stevenmagdy Which phone did you test on? and was this the only thing? Other suggestions?

Muddz commented

@Stevenmagdy This is a bug in the Android code. Reported here. https://issuetracker.google.com/issues/71483403

Else it should have worked