Android O introduced fonts in xml back to API 16 via the support library. However I am unable to find the support equivalent of TypedArray.getFont()
, which required API level 26.
val array = context.obtainStyledAttributes(styleResId, R.styleable.TextAppearance)
val font = array.getFont(R.styleable.TextAppearance_android_fontFamily) // nope
Is there some kind of compat utility class I can use to retrieve a font from a style resource id?
Found a workaround which is to find the resource ID of the font from the TypedArray
, and then use ResourcesCompat
to load the font.
val array = context.obtainStyledAttributes(styleResId, R.styleable.TextAppearance)
if (array.hasValue(R.styleable.TextAppearance_android_fontFamily)) {
val fontId = array.getResourceId(R.styleable.TextAppearance_android_fontFamily, -1)
val typeface = ResourcesCompat.getFont(context, fontId)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With