Android Jetpack Compose – Text Font Weight
To set a font weight for text in Text composable in Android Jetpack Compose, you can set the fontWeight parameter of the Text composable with required FontWeight (androidx.compose.ui.text.font.FontWeight) value.
For example, following are some of the allowed FontWeight values.
- ExtraBold
- Bold
- SemiBold
- Normal
- Medium
- Thin
- Light
- ExtraLight
- W100
- W200
- W300
- W400
- W500
- W600
- W700
- W800
- W900
Examples
1. Text font weight set to FontWeight.ExtraBold
In the following example, we have set the font weight of text in Text composable to ExtraBold
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.FontWeight
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,
fontWeight = FontWeight.ExtraBold
)
}
}
}
}
}
Screenshot
2. Text font weight set to FontWeight.Normal
In the following example, we have set the font weight of text in Text composable to Normal.
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.FontWeight
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,
fontWeight = FontWeight.Normal
)
}
}
}
}
}
Screenshot
3. Text font weight set to FontWeight.ExtraLight
In the following example, we have set the font weight of text in Text composable to ExtraLight.
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.FontWeight
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,
fontWeight = FontWeight.ExtraLight
)
}
}
}
}
}
Screenshot
Summary
In this tutorial, we have seen how to set font weight for text in Text composable in Android Jetpack Compose, using fontWeight parameter of Text composable.