Set Text Size in Android Jetpack Compose Text
In this article
You set text size in Jetpack Compose with the fontSize parameter on the Text composable, or by applying a TextStyle that carries a fontSize value. The quickest working call is Text("Hello", fontSize = 20.sp). Use .sp (scale-independent pixels) so the text respects the user's system font-size preference, and use TextStyle when the same size needs to be reused or combined with weight, color, and line height. This guide walks through the exact options, the reasons behind them, and the mistakes that make Compose text ignore accessibility settings or overflow its container.
What You'll Need
- Android Studio with a project that already uses Jetpack Compose (a Compose Activity template is enough).
- A
Textcomposable in a@Composablefunction, or a new one you add for testing. - Basic familiarity with Kotlin and importing Compose UI packages such as
androidx.compose.ui.unit.spandandroidx.compose.ui.text.TextStyle. - An emulator or device to preview the result; the Compose preview pane is optional but useful.
Step-by-Step
- Import the unit extension. At the top of your Kotlin file, make sure
androidx.compose.ui.unit.spis imported. Without it,20.spwill not resolve and the compiler will complain about an unresolved reference. Android Studio usually adds it automatically when you type.sp, but check the import block if you see an error. - Set the size directly on Text. Add the
fontSizeparameter to the composable:Text(text = "Hello", fontSize = 20.sp). This is the shortest path and is ideal for one-off labels. The value is aTextUnit, so you must use.sp(or.emfor relative sizing), not a plain number. - Move to a TextStyle for reuse. When several Text composables share a size, define a style:
val bodyStyle = TextStyle(fontSize = 18.sp, fontWeight = FontWeight.Normal)and pass it withText("Hello", style = bodyStyle). ATextStylealso acceptslineHeight,letterSpacing, andcolor, so it keeps related typography in one place instead of scattering parameters across calls. - Prefer theme typography for app-wide consistency. In Material 3, wrap your UI in
MaterialThemeand read sizes fromMaterialTheme.typography.bodyLarge,titleMedium, and so on. To change a single size without abandoning the theme, copy the style:style = MaterialTheme.typography.bodyLarge.copy(fontSize = 22.sp). This preserves the theme's weight and line height while overriding only the size. - Use
.emwhen the size should scale with the parent style. If a style is already 16.sp,fontSize = 1.5.emrenders at 24.sp and stays proportional if the base style changes later. This is useful for headings derived from body text, but avoid mixing.spand.emcarelessly in the same hierarchy or the visual rhythm becomes hard to predict. - Check how the text behaves with system font scaling. Because
.sprespects the user's font-size setting, a 20.sp label can grow noticeably when the device is set to a large font. Test by changing the system font size in the device settings (labels vary by version and manufacturer) and confirm the layout still fits. If a size must never scale, use.dpviafontSize = with(LocalDensity.current) { 20.dp.toSp() }, but reserve that for cases where scaling would break the design, since it ignores accessibility preferences. - Handle overflow deliberately. Larger text can exceed its container. Set
maxLinesandoverflow = TextOverflow.Ellipsison Text, or let it wrap by leavingmaxLinesunset. For single-line buttons,maxLines = 1with ellipsis prevents the button from growing unpredictably. - Preview and verify. Use
@PreviewwithfontScaleparameters (for example@Preview(fontScale = 1.3f)) to see how the text renders at larger accessibility scales before running on a device. Adjust sizes or constraints until nothing clips.
Common Mistakes
- Passing a raw number.
fontSize = 20does not compile becausefontSizeexpects aTextUnit. Always attach.spor.em. - Using
.dpfor text..dpdoes not scale with the user's font-size setting, so text stays small for users who need larger type. Use.spunless you have a specific reason not to. - Overriding the theme everywhere. Hard-coding sizes on every Text breaks consistency and makes a later design change a hunt across files. Prefer theme typography and
.copy(fontSize = ...)for exceptions. - Forgetting line height. A large
fontSizewith the default line height can look cramped on multi-line paragraphs. SetlineHeightin the sameTextStyle, typically around 1.2 to 1.5 times the font size. - Ignoring font scale in previews. A layout that looks fine at 1.0 can clip at 1.3 or 2.0. Test with the preview
fontScaleparameter or the device's system setting.
Tips
- Keep a small set of named text styles (body, label, title) and reuse them; fewer sizes make the UI calmer and easier to maintain.
- Use
MaterialTheme.typographyas the base and only overridefontSizewhen a screen genuinely needs a different scale. - When mixing
.spand.em, document the intent in a comment so the next developer knows which values are absolute and which are relative. - For text inside a fixed-height container, check both the smallest and largest system font settings; if it cannot fit, allow wrapping or ellipsis rather than shrinking the font below accessibility guidance.
- Remember that Compose previews and the running app can differ slightly in font rendering; confirm final sizes on a real device.
Troubleshooting
- "Unresolved reference: sp". Add the import
androidx.compose.ui.unit.sp. If it still fails, confirm the Compose UI dependency is present in the module's build file. - Text looks the same size after a change. A parent
TextStyleor theme style may be overriding the value, or you edited a different Text composable. Check thestyleparameter and search for duplicate composables. - Text is clipped at large system font sizes. Remove fixed heights, allow wrapping, or add
maxLineswith ellipsis. Avoid forcing a smaller size just to fit; adjust the layout instead. - Size changes do not appear in preview. Refresh the preview or rebuild the project; stale previews are common after editing style definitions in another file.
- Text overflows a button or chip. Set
maxLines = 1andoverflow = TextOverflow.Ellipsis, and check that the container uses wrap content rather than a hard-coded width.
Frequently Asked Questions
Should I use sp or dp for text size in Compose?
Use .sp. It scales with the user's font-size preference, which is the expected behavior for text. .dp is for layout dimensions, not type.
Can I set text size globally for the whole app?
Yes, through MaterialTheme.typography. Define your typography scale once, wrap the app in MaterialTheme, and read styles from MaterialTheme.typography in each Text composable.
How do I make text size relative to the parent style?
Use .em, for example fontSize = 1.5.em. It multiplies the inherited font size, so the text stays proportional if the base style changes.
Why does my text grow when the user changes system font size?
Because .sp is scale-independent and respects that setting. This is intended accessibility behavior. If a specific element must not scale, convert a dp value to sp with LocalDensity, but use that sparingly.
Can I set different sizes for different screen sizes?
Yes. Read the current configuration or use window size classes, then choose a size or style accordingly. Keep the logic in one place so sizes stay consistent across screens.
Does fontSize accept negative or zero values?
No. Use a positive value. Zero or negative sizes are not meaningful for rendered text and will not display as expected.