Android Jetpack Compose – Text Font Family
To set a font family for text in Text composable in Android Jetpack Compose, you can set the fontFamily parameter of the Text composable with required FontFamily (androidx.compose.ui.text.font.FontFamily) value.
For example, following are some of the allowed FontFamily values.
- Cursive
- Monospace
- Default
- Serif
- SansSerif
Examples
1. Text font family set to FontFamily.Monospace
In the following example, we have set the font family of text in Text composable to Monospace.
MainActivity.kt
package com.example.myapplication
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.sp
import com.example.myapplication.ui.theme.MyApplicationTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Text(
text = "Hello Android!",
fontSize = 30.sp,
fontFamily = FontFamily.Monospace
)
}
}
}
}
}
Screenshot
2. Text font family set to FontFamily.Cursive
In the following example, we have set the font family of text in Text composable to Cursive.
MainActivity.kt
package com.example.myapplication
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.sp
import com.example.myapplication.ui.theme.MyApplicationTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Text(
text = "Hello Android!",
fontSize = 30.sp,
fontFamily = FontFamily.Cursive
)
}
}
}
}
}
Screenshot
Summary
In this tutorial, we have seen how to set font family for text in Text composable in Android Jetpack Compose, using fontFamily parameter of Text composable.