androidx.compose.ui.text.googlefonts

Classes

GoogleFont

A downloadable font from fonts.google.com

GoogleFont.Provider

Attributes used to create a FontRequest for a GoogleFont based Font.

Top-level functions summary

Font
Font(
    googleFont: GoogleFont,
    weight: FontWeight,
    style: FontStyle,
    variationSettings: FontVariation.Settings
)

Load a font from Google Fonts via Downloadable Fonts using the default GoogleFont.Provider.

Font
Font(
    googleFont: GoogleFont,
    fontProvider: GoogleFont.Provider,
    weight: FontWeight,
    style: FontStyle,
    variationSettings: FontVariation.Settings
)

Load a font from Google Fonts via Downloadable Fonts.

GoogleFont
GoogleFont(context: Context, fontXml: @FontRes Int)

Load a Google Font from XML

Extension functions summary

Boolean

Check if the downloadable fonts provider is available on device.

Top-level functions

fun Font(
    googleFont: GoogleFont,
    weight: FontWeight = FontWeight.Normal,
    style: FontStyle = FontStyle.Normal,
    variationSettings: FontVariation.Settings = FontVariation.Settings(weight, style)
): Font

Load a font from Google Fonts via Downloadable Fonts using the default GoogleFont.Provider.

If the requested GoogleFont is available as a variable font in Google Fonts and the non-empty FontVariation.Settings is provided for variationSettings, the returned Font will be a variable font. Variable font settings are only applied on API level 26 and above.

This overload function simplifies the setup by automatically configuring the GoogleFont.Provider with the default font certificates required to fetch fonts from Google Play Services.

import androidx.compose.material.Text
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontVariation
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.googlefonts.Font
import androidx.compose.ui.text.googlefonts.GoogleFont

val fontFamily =
    FontFamily(
        Font(
            googleFont = GoogleFont("Lobster Two"),
            weight = FontWeight.W600,
            style = FontStyle.Italic,
            variationSettings = FontVariation.Settings(),
        )
    )

Text("Hello World", style = TextStyle(fontFamily = fontFamily))
import androidx.compose.material.Text
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontVariation
import androidx.compose.ui.text.googlefonts.Font
import androidx.compose.ui.text.googlefonts.GoogleFont

val fontVariationSettings =
    FontVariation.Settings(
        FontVariation.grade(0),
        FontVariation.weight(900),
        FontVariation.slant(0f),
        FontVariation.width(100f),
    )

val fontFamily =
    FontFamily(
        Font(
            googleFont = GoogleFont("Google Sans Flex"),
            variationSettings = fontVariationSettings,
        )
    )

Text("Hello World", style = TextStyle(fontFamily = fontFamily))
Parameters
googleFont: GoogleFont

A font to load from fonts.google.com

weight: FontWeight = FontWeight.Normal

font weight to load

style: FontStyle = FontStyle.Normal

italic or normal font

variationSettings: FontVariation.Settings = FontVariation.Settings(weight, style)

The FontVariation.Settings to apply to the variable font.

fun Font(
    googleFont: GoogleFont,
    fontProvider: GoogleFont.Provider,
    weight: FontWeight = FontWeight.Normal,
    style: FontStyle = FontStyle.Normal,
    variationSettings: FontVariation.Settings = FontVariation.Settings()
): Font

Load a font from Google Fonts via Downloadable Fonts.

This function allows specifying a custom font provider. For most common use cases with Google Play Services, consider using the overload that omits the fontProvider parameter.

If the requested GoogleFont is available as a variable font in Google Fonts and the non-empty FontVariation.Settings is provided for variationSettings, the returned Font will be a variable font. Variable font settings are only applied on API level 26 and above.

import androidx.compose.material.Text
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.googlefonts.Font
import androidx.compose.ui.text.googlefonts.GoogleFont
import androidx.compose.ui.text.googlefonts.R

val fontProvider =
    GoogleFont.Provider(
        providerAuthority = "com.google.android.gms.fonts",
        providerPackage = "com.google.android.gms",
        certificates = R.array.com_google_android_gms_fonts_certs,
    )

val fontFamily =
    FontFamily(
        Font(
            googleFont = GoogleFont("Google Sans Flex"),
            weight = FontWeight.W600,
            fontProvider = fontProvider,
        )
    )

Text("Hello World", style = TextStyle(fontFamily = fontFamily))
import androidx.compose.material.Text
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontVariation
import androidx.compose.ui.text.googlefonts.Font
import androidx.compose.ui.text.googlefonts.GoogleFont
import androidx.compose.ui.text.googlefonts.R

val fontVariationSettings =
    FontVariation.Settings(
        FontVariation.grade(0),
        FontVariation.weight(900),
        FontVariation.slant(0f),
        FontVariation.width(100f),
    )

val fontProvider =
    GoogleFont.Provider(
        providerAuthority = "com.google.android.gms.fonts",
        providerPackage = "com.google.android.gms",
        certificates = R.array.com_google_android_gms_fonts_certs,
    )

val fontFamily =
    FontFamily(
        Font(
            googleFont = GoogleFont("Google Sans Flex"),
            fontProvider = fontProvider,
            variationSettings = fontVariationSettings,
        )
    )

Text("Hello World", style = TextStyle(fontFamily = fontFamily))
Parameters
googleFont: GoogleFont

A font to load from fonts.google.com

fontProvider: GoogleFont.Provider

configuration for downloadable font provider

weight: FontWeight = FontWeight.Normal

font weight to load

style: FontStyle = FontStyle.Normal

italic or normal font

variationSettings: FontVariation.Settings = FontVariation.Settings()

The FontVariation.Settings to apply to the variable font.

fun GoogleFont(context: Context, fontXml: @FontRes Int): GoogleFont

Load a Google Font from XML

This will load only the name and besteffort parameters.

Compared to the string constructor, this loader adds additional overhead. New code should prefer to use GoogleFont(name: String).

Parameters
context: Context

to load font from

fontXml: @FontRes Int

fontRes to load

Throws
IllegalArgumentException

if the fontRes does not exist or is not an xml GoogleFont

Extension functions

GoogleFont.Provider.isAvailableOnDevice

@WorkerThread
fun GoogleFont.Provider.isAvailableOnDevice(context: Context): Boolean

Check if the downloadable fonts provider is available on device.

This is not necessary for normal usage, but may be useful in debugging downloadable fonts behavior.

Parameters
context: Context

for looking up font provider in

Returns
Boolean

true if the provider is usable for downloadable fonts, false if it's not found

Throws
IllegalStateException

if the provider is on device, but certificates don't match