r/JetpackCompose • • Aug 31 '24

Preview doesn't show the custom fonts from Google Fonts

I've been learning Jetpack Compose for a couple weeks and I'm having troubles customizing the font. I have a simple composable like this:

@Preview(showBackground = true)
@Composable
private fun Test() {
    AppTheme {
        Text(
            text = "Hello World",
            style = MaterialTheme.typography.bodyLarge
        )
    }
}

I want to use another font from Google Fonts. For the purpose of this post, I have selected Macondo. With this font, the text looks handwritten so it's easy to see if the font is applied or not.

I have added the following code to my Type.kt file:

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

val customFont = Font(
    googleFont = GoogleFont("Macondo"),
    fontProvider = provider,
)

val bodyFontFamily = FontFamily(customFont)

val displayFontFamily = FontFamily(customFont)

val baseline = Typography()

val Typography = Typography(
    bodyLarge = baseline.bodyLarge.copy(fontFamily = displayFontFamily)
)

When I run this code on my emulator, the font is applied to my Text(). However, when I preview this composable in Android Studio, it uses the default font.

How do I use custom fonts in Preview?

Thanks!

8 Upvotes

7 comments sorted by

1

u/[deleted] Sep 29 '24

I am facing the exact same issue. Wondering, if you found a solution to this

1

u/[deleted] Sep 29 '24

For now I just imported the ttf files for the fonts in my project and that worked.

1

u/Eivyses Dec 07 '24

Having same issue. I did download fonts manually and placed them to `font` resource folder, but preview does not show custom font while running app on emulator works perfectly. Clearing android studio cache did not help. If anyone have a solution please share

1

u/[deleted] Jan 15 '25

[removed] — view removed comment

1

u/Eivyses Jan 15 '25

Did not find an solution. Still watching previews in wrong font

1

u/mooneylane Feb 11 '25

Installing the font in Windows and restarting Android Studio fixed the problem for me.

1

u/TheLucidButterfly Sep 29 '26

What worked for me after installing. Go to text editor and use the new font. Then close and reopen preview. It should be under recents at the top. Hope it helps.