TechBuzzPro

Set Text Size in Android Jetpack Compose Text

· 6 min read · Phones & Tablets

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 Text composable in a @Composable function, or a new one you add for testing.
  • Basic familiarity with Kotlin and importing Compose UI packages such as androidx.compose.ui.unit.sp and androidx.compose.ui.text.TextStyle.
  • An emulator or device to preview the result; the Compose preview pane is optional but useful.

Step-by-Step

  1. Import the unit extension. At the top of your Kotlin file, make sure androidx.compose.ui.unit.sp is imported. Without it, 20.sp will 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.
  2. Set the size directly on Text. Add the fontSize parameter to the composable: Text(text = "Hello", fontSize = 20.sp). This is the shortest path and is ideal for one-off labels. The value is a TextUnit, so you must use .sp (or .em for relative sizing), not a plain number.
  3. 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 with Text("Hello", style = bodyStyle). A TextStyle also accepts lineHeight, letterSpacing, and color, so it keeps related typography in one place instead of scattering parameters across calls.
  4. Prefer theme typography for app-wide consistency. In Material 3, wrap your UI in MaterialTheme and read sizes from MaterialTheme.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.
  5. Use .em when the size should scale with the parent style. If a style is already 16.sp, fontSize = 1.5.em renders at 24.sp and stays proportional if the base style changes later. This is useful for headings derived from body text, but avoid mixing .sp and .em carelessly in the same hierarchy or the visual rhythm becomes hard to predict.
  6. Check how the text behaves with system font scaling. Because .sp respects 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 .dp via fontSize = with(LocalDensity.current) { 20.dp.toSp() }, but reserve that for cases where scaling would break the design, since it ignores accessibility preferences.
  7. Handle overflow deliberately. Larger text can exceed its container. Set maxLines and overflow = TextOverflow.Ellipsis on Text, or let it wrap by leaving maxLines unset. For single-line buttons, maxLines = 1 with ellipsis prevents the button from growing unpredictably.
  8. Preview and verify. Use @Preview with fontScale parameters (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 = 20 does not compile because fontSize expects a TextUnit. Always attach .sp or .em.
  • Using .dp for text. .dp does not scale with the user's font-size setting, so text stays small for users who need larger type. Use .sp unless 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 fontSize with the default line height can look cramped on multi-line paragraphs. Set lineHeight in the same TextStyle, 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 fontScale parameter 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.typography as the base and only override fontSize when a screen genuinely needs a different scale.
  • When mixing .sp and .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 TextStyle or theme style may be overriding the value, or you edited a different Text composable. Check the style parameter and search for duplicate composables.
  • Text is clipped at large system font sizes. Remove fixed heights, allow wrapping, or add maxLines with 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 = 1 and overflow = 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.