0

What string do we pass into androidx.glance.text.FontFamily to specify a custom font? For example, if I imported @font/open_sans_regular, is the family supposed to be "open_sans_regular"?

/**
 * Describes the family of the font.
 * Defaults are provided, but it is also possible to supply a custom family. If this is found on
 * the system it will be used, otherwise it will fallback to a system default.
 */
class FontFamily constructor(val family: String) {
    companion object {
        /**
         * The formal text style for scripts.
         */
        val Serif = FontFamily("serif")

        /**
         * Font family with low contrast and plain stroke endings.
         */
        val SansSerif = FontFamily("sans-serif")

        /**
         * Font family where glyphs have the same fixed width.
         */
        val Monospace = FontFamily("monospace")

        /**
         * Cursive, hand-written like font family.
         */
        val Cursive = FontFamily("cursive")
    }

    override fun toString(): String {
        return family
    }
}
VIN
  • 6,385
  • 7
  • 38
  • 77

1 Answers1

0

When you are working with widgets on Android, you can't use a custom font. You can specify one of several supported font families. In Glance you use the constants on the FontFamily class. For example

Text(style = TextStyle(
                 fontFamily = FontFamily.Cursive
             ),
     text = "Hello World"
)

FontFamily provides the following default families : Cursive, Monospace, Serif, and SansSerif.

The constructor allows you to specify custom system supported font families. The full list of font-families your device supports can be found in /system/etc/fonts.xml in API 21+.

For example,

Text(style = TextStyle(
                 fontFamily = FontFamily("casual")
             ),
     text = "Hello World"
)

Will use casual font family on devices which support that. On devices which don't support the provided family Glance defaults to a default system font.

  • The constructor for `FontFamily` is public so I assumed that a custom font is allowed. Note that the kdoc above says "Defaults are provided, but it is also possible to supply a custom family." – VIN Jun 16 '23 at 22:10