Search code examples
material-designandroid-jetpack-composeandroid-jetpack-navigationandroid-typeface

New to Android development. Why is my custom typeface font not showing up in the Type folder?


I'm pretty new to Android development (especially Jetpack Compose), so I'm sure I'm forgetting something stupidly easy!

I downloaded the custom Montserrat regular and bold fonts from Google (as .ttf files), created a font directory in my resources folder, renamed the fonts to follow naming conventions, dragged the fonts into the font directory, then tried to initialize them in the Type.kt file as a variable named Montserrat. I get an unresolved reference error and the Lint suggestion creates an XML file in the font directory. I have used custom fonts from Google many times before and followed the exact steps, but something is clearly not working!

I'd love to post screenshots, but I do not have enough reputation, unfortunately.

Any help is extremely appreciated!

Followed basic instructions to use custom fonts, and it is not initializing in the Type.kt folder.


Solution

  • If you're using Jetpack Compose to build your Android app, you can specify custom fonts in your app's typography object. You can then use the font property in the Text composable to apply the font to your text. Here's an example

    @Composable
    fun MyApp() {
      val customFont = font(R.font.montserrat)
      Typography(
        h1 = TextStyle(
          font = customFont,
          fontSize = 24.sp
        )
      )
      // ...
    }
    
    

    You can then use this style in your app like this

    @Composable
    fun MyScreen() {
      Text("My headline", style = Typography.h1)
    }