23 July 2024

How to use Material Themebuilder generated themes with bundled fonts in an Android Compose project

When using Material theme builder, kotlin theme files are generated using downloadable Google fonts.

I wanted to change these fonts which are dynamically downloaded by the running application with fonts that were bundled with my Android Studio Kotlin Compose application. Here are the steps to do replace the downloadable fonts

  1. Download the kotlin theme and integrate it in your application's theme folder
  2. Download the font
      • Alternatively work with any other ttf file you want to replace the downloadable Google fonts with
  3. Add the font to your project. (Elaborate instructions on adding fonts to your compose project here)
    1. In your project create a font folder under res (if it does not exist yet)
    2. Put the downloaded ttf files in this folder. 
    3. Replace all dash characters "-" in the filename with underscores "_". Replace all uppercase characters in the filename with lowercace (optionally preceded by an underscore).
  4.  Adapt ...ui.theme.Type.kt in your project
    1. Replace all occurences of
      Font(
      googleFont = GoogleFont("theFontName"),
      fontProvider = provider,
      )
      with
      Font(
      R.font.the_font_filename
      )
    2. remove the provider variable and its initialisation

Test and run! 

Additional cookbooks on adding fonts to your compose project:


No comments:

Post a Comment