In order to use custom fonts in an android app there seem to be two approaches:
- Classic way: place TTF or OTF files in the
/assets/fonts
directory and then build aTypeface
withTypeface.createFromAsset(getAssets(), "fonts/custom.ttf")
. - Natively since API 26, or with AppCompat since API 16: create an XML font family by placing lowercased TTF/OTF files in
res/font
folder and then reference them directly in XML layouts withandroid:fontFamily="@font/custom"
, or access them programatically withResourcesCompat.getFont(this, R.font.custom)
.
What are the key differences to keep in mind between font resources and assets?
Specifically, are they rendered in the same way, and is any of them faster or more efficient in terms of performance?
Can it be assumed that font resources are only suitable for fonts that come pre-packaged in the APK, while font assets are more flexible since you can create a Typeface from an arbitrary file inside or outside the APK?
Update:
After a bit of experimentation it looks like font resources are the only way to set custom fonts in AppWidget TextView
s without having to manually paint them as bitmaps but that requires the device to actually run API 26 (using the Support Library does not help in this specific case).