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)

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
)

Load a font from Google Fonts via Downloadable Fonts.

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

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

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

Load a font from Google Fonts via Downloadable Fonts, including variable font settings.

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
): Font

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

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.

To learn more about the features supported by Google Fonts, see Get Started with the Google Fonts for Android

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.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,
        )
    )

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

fun Font(
    googleFont: GoogleFont,
    fontProvider: GoogleFont.Provider,
    weight: FontWeight = FontWeight.Normal,
    style: FontStyle = FontStyle.Normal
): 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.

To learn more about the features supported by Google Fonts, see Get Started with the Google Fonts for Android

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

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

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

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.

Variable font settings are only applied on API level 26 and above.

To learn more about the features supported by Google Fonts, see Get Started with the Google Fonts for Android

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

variationSettings: FontVariation.Settings

The FontVariation.Settings to apply to the variable font.

weight: FontWeight = FontWeight.Normal

font weight to load

style: FontStyle = FontStyle.Normal

italic or normal font

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

Load a font from Google Fonts via Downloadable Fonts, including variable font settings.

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.

Variable font settings are only applied on API level 26 and above.

To learn more about the features supported by Google Fonts, see Get Started with the Google Fonts for Android

Parameters
googleFont: GoogleFont

A font to load from fonts.google.com

fontProvider: GoogleFont.Provider

configuration for downloadable font provider

variationSettings: FontVariation.Settings

The FontVariation.Settings to apply to the variable font.

weight: FontWeight = FontWeight.Normal

font weight to load

style: FontStyle = FontStyle.Normal

italic or normal 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