100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Text and Typography

The Text composable and Material 3's type scale let you render and style text consistently — controlling font, weight, size, color, and overflow — across an entire Compose app.

Building UIs with ComposeBeginner7 min readJul 8, 2026
Analogies

Text and Typography

The Text composable is Compose's primary way to render strings on screen. Beyond a simple Text("Hello"), it accepts parameters for color, fontSize, fontWeight, fontStyle, textAlign, maxLines, and overflow behavior, plus a style: TextStyle parameter for applying a complete, reusable typographic definition at once — most commonly one pulled from MaterialTheme.typography.

🏏

Cricket analogy: A commentator can describe a shot with basic words, or reach for a full pre-set phrase like 'textbook cover drive' that packages tone, emphasis, and pacing all at once — like passing individual params to Text versus applying a complete style: TextStyle pulled from a shared typography scale.

Material 3's Type Scale

Material Design 3 defines a type scale of named styles — displayLarge, headlineMedium, titleSmall, bodyLarge, labelMedium, and so on — each a TextStyle bundling font size, line height, letter spacing, and weight for a specific UI role. Using MaterialTheme.typography.titleMedium instead of hardcoding fontSize = 18.sp keeps text visually consistent across an app and lets a single theme change cascade everywhere instantly.

🏏

Cricket analogy: A cricket board defines named formats — Test, ODI, T20 — each bundling a fixed set of rules (overs, follow-on, powerplay) rather than negotiating rules match by match; using MaterialTheme.typography.titleMedium instead of hardcoding fontSize = 18.sp is like choosing 'T20' instead of inventing custom rules each time.

kotlin
@Composable
fun ArticlePreview(title: String, summary: String, author: String) {
    Column(modifier = Modifier.padding(16.dp)) {
        Text(
            text = title,
            style = MaterialTheme.typography.headlineSmall,
            fontWeight = FontWeight.Bold,
            maxLines = 2,
            overflow = TextOverflow.Ellipsis
        )
        Spacer(modifier = Modifier.height(4.dp))
        Text(
            text = summary,
            style = MaterialTheme.typography.bodyMedium,
            color = MaterialTheme.colorScheme.onSurfaceVariant,
            maxLines = 3,
            overflow = TextOverflow.Ellipsis
        )
        Text(
            text = "By $author",
            style = MaterialTheme.typography.labelSmall,
            fontStyle = FontStyle.Italic
        )
    }
}

Overflow, Line Limits, and Selectable Text

maxLines combined with overflow = TextOverflow.Ellipsis truncates long text with a trailing ellipsis rather than letting it overflow its container. For text the user should be able to copy, wrap the composable in SelectionContainer. For rich, mixed-styling text within a single block (e.g. bold a substring), use buildAnnotatedString to construct an AnnotatedString with per-range SpanStyles.

🏏

Cricket analogy: A scoreboard showing a long player name gets truncated to 'D. Warner...' rather than overflowing the display (maxLines + ellipsis); a stat sheet the fan can copy into notes needs selectable text; a commentary line bolding just the word 'SIX' within a full sentence needs per-range styling like buildAnnotatedString.

sp (scale-independent pixels) is used for font sizes instead of dp because sp additionally scales with the user's system font-size accessibility setting, while dp does not — this is why you should never hardcode font sizes in dp.

Overriding individual parameters like fontSize directly alongside a style parameter works (explicit parameters win), but doing this pervasively defeats the purpose of a centralized type scale and makes future theme changes inconsistent. Prefer defining a new named TextStyle in the theme if a look is reused often.

  • Text is Compose's core composable for rendering strings, with rich styling parameters.
  • MaterialTheme.typography exposes a named type scale (headlineSmall, bodyMedium, etc.) for consistency.
  • maxLines + TextOverflow.Ellipsis truncates long text gracefully.
  • SelectionContainer enables user text selection/copy for wrapped Text composables.
  • buildAnnotatedString supports mixed styling (e.g. bold substrings) within one text block.
  • Font sizes should use sp, not dp, so text respects the user's system accessibility font scale.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#AndroidWithJetpackComposeStudyNotes#MobileDevelopment#TextAndTypography#Text#Typography#Material#Type#StudyNotes#SkillVeris