Search code examples
android-jetpack-composeandroid-jetpack-compose-text

Using sans-serif-condensed in compose


I'm want to use sans-serif-condensed in jetpack compose, but for the life of me, I can't find it anywhere within compose itself?

Am I just blind, or will I need to do something like import the font and set it all manually?


Solution

  • You can use the fontFamily attribute to specify the font family.

    Text("Hello World", fontFamily = FontFamily.Serif)
    

    Predefined Fonts

    By default, Jetpack compose has only 5 Font Families,
    Default, SansSerif, Serif, Monospace and Cursive.

    Custom Fonts

    If you need any other font family (which seems to be your case), you have to add it as mentioned here.

    Steps

    1. Download or Get the required fonts files and add them to the res/fonts directory.

    2. Create FontFamily like this,

       val firaSansFamily = FontFamily(
           Font(R.font.firasans_light, FontWeight.Light),
           Font(R.font.firasans_regular, FontWeight.Normal),
           Font(R.font.firasans_italic, FontWeight.Normal, FontStyle.Italic),
           Font(R.font.firasans_medium, FontWeight.Medium),
           Font(R.font.firasans_bold, FontWeight.Bold)
       )
      
    3. Use like this,

       Text(..., fontFamily = firaSansFamily, fontWeight = FontWeight.Light)
       Text(..., fontFamily = firaSansFamily, fontWeight = FontWeight.Normal)