If you’re starting Android UI with Jetpack Compose, you’ll love how quickly you can build clean interfaces without XML. In this step-by-step guide, we’ll show you exactly how to create Jetpack Compose Text and Button UI using Material 3, style them with themes and typography, handle clicks, and preview everything right inside Android Studio. This tutorial is written for students, beginners, and anyone learning Kotlin/Java for Android app development.
What is Jetpack Compose and why use it for Android UI?
Jetpack Compose is Android’s modern toolkit for building native UI with Kotlin. Instead of writing XML layouts, you describe UI with composable functions. It’s:
- Declarative: UI is a function of state, so UI updates automatically when data changes.
- Kotlin-first: No XML or findViewById needed.
- Material 3 ready: Use theme-aware components like
TextandButtonthat adapt to light/dark themes and accessibility settings.
For beginners, Compose reduces boilerplate and makes learning Android UI much more approachable. If you’re looking for Jetpack Compose basics for beginners, mastering Text and Button is the perfect starting point.
Project setup (Material 3 + Compose BOM)
Add the Compose BOM to keep versions aligned and include Material 3 components. In your app module’s build.gradle (Kotlin DSL shown), add:
Tip: Always check the latest Compose BOM version in the Android docs or release notes. We’ll use Material 3 (androidx.compose.material3) since it’s the recommended design system for new apps.
Create a minimal Compose screen with Text and Button
Below is a simple screen that shows a greeting and a button that increases a counter on click. This is a common example for a Jetpack Compose tutorial for Kotlin beginners.
MainActivity (Kotlin)
How Text works in Compose
The Text composable is a Material 3 component that automatically picks typography and colors from MaterialTheme. This makes your app theme-aware and accessible by default. Here are common properties you’ll use:
text: The string to display.style: UseMaterialTheme.typography(e.g.,titleLarge,bodyMedium).color: FromMaterialTheme.colorScheme(e.g.,onBackground).maxLinesandoverflow: Handle long text with ellipses.fontSize,fontWeight,letterSpacing: Direct font styling when needed.
Text with ellipsis and modern overflow modes
Auto-size text to fit
If a container is constrained (like a card or chip), you can auto-size text so it fits nicely. Use BasicText with TextAutoSize:
Links inside Text (without deprecated ClickableText)
To add a tappable link in text, build an AnnotatedString with LinkAnnotation and show it using Text or BasicText. This replaces the now-deprecated ClickableText.
This approach integrates better with accessibility and Material semantics while keeping your code clean and modern.
Create and style Buttons in Compose
Material 3 offers multiple button types. Use the one that matches the emphasis you want:
- Button: Primary, filled.
- FilledTonalButton: Secondary emphasis.
- ElevatedButton: Subtle elevation.
- OutlinedButton: Low emphasis with outline.
- TextButton: Minimal emphasis, text-only.
Examples with onClick
Customize colors, shape, and padding
Best practice: keep interactive targets at least 48 dp in height/width, and prefer using Material components because they meet accessibility guidelines by default.
Put it together: a simple Compose screen, step by step
Let’s build a compact screen that demonstrates the following:
- Title with ellipsis
- Auto-sized subtitle
- Primary button that changes state
- Secondary outlined action
When to use Text vs. BasicText
- Text: Use for most app text. It integrates with Material 3 theming, typography, and colors.
- BasicText: Use for advanced scenarios (e.g., auto-size text, custom layout behavior) when you don’t need Material styling.
Accessibility and theming tips
- Use sp for font sizes so the user’s system settings scale your text appropriately.
- Prefer theme styles:
MaterialTheme.typography.*andMaterialTheme.colorScheme.*to keep design consistent and accessible. - Buttons already merge label semantics; you don’t need to set
contentDescriptionfor the text inside aButton. - Maintain minimum 48 dp touch targets.
- Dependencies set: Compose BOM, Material 3, Activity Compose, Tooling Preview.
- Wrap your UI with
MaterialTheme { ... }so Text and Button use proper colors/typography. - Use
Textwithstyle = MaterialTheme.typography.*and handle long strings viamaxLines+TextOverflow. - Create actions with
Button(onClick = { ... }) { Text("Label") }; keep height ≥ 48.dp. - Store UI state with
remember { mutableStateOf(...) }so Text updates when Button is clicked. - Prefer theme colors (
MaterialTheme.colorScheme) over hard-coded colors. - Add
@Previewcomposables for quick iteration; test dark mode withuiMode. - For links in text, use
LinkAnnotationwithText/BasicText(avoid deprecatedClickableText). - Use
BasicText+TextAutoSizewhen text must fit tight spaces.
Preview and test your Compose UI in Android Studio
Compose Previews let you see your composables without running the app. You can even preview dark mode.
To test behavior like onClick, use Compose UI tests with createAndroidComposeRule and performClick on nodes matching text or test tags. For beginners, starting with Previews is often enough to iterate quickly.
Troubleshooting and common mistakes
- Text doesn’t fit or gets cut off: Set
maxLinesandoverflow, or useBasicTextwithTextAutoSize. - Colors look off: Ensure you’re using
MaterialTheme.colorSchemeand not hard-coded colors, unless necessary. - Buttons feel small or hard to tap: Ensure at least 48 dp height/width. Material 3 buttons include good defaults, but constraints can reduce size if you’re not careful.
- Deprecated APIs for links: Don’t use
ClickableTextfor hyperlinks; useLinkAnnotationwithText/BasicTextinstead.
Output / Result
After implementing the code above, you’ll see:
- A themed screen with a title that truncates gracefully with an ellipsis.
- An auto-sized subtitle that adapts to the container width.
- A primary button that toggles state (e.g., Subscribe ↔ Unsubscribe) with each tap.
- A secondary outlined button for a less prominent action.
This is the core of how to add text and button in Jetpack Compose and a solid foundation to build screens quickly.
FAQ
What is Jetpack Compose and how does it work for Android UI?
Jetpack Compose is Android’s modern, declarative UI toolkit. You build UI with Kotlin functions called composables. Compose updates the UI automatically when state changes, which reduces boilerplate and helps you move faster.
How do I add a Text composable in Jetpack Compose?
Use Text("Hello"). To keep it theme-aware, apply style = MaterialTheme.typography.* and colors from MaterialTheme.colorScheme. You can control maxLines and overflow to handle long text.
How do I create a Button with an onClick action in Compose?
Use Button(onClick = { /* action */ }) { Text("Click") }. Keep actions simple, and store UI state with remember { mutableStateOf(...) } so the UI updates automatically when users tap. For example: var count by remember { mutableStateOf(0) } then onClick = { count++ }.
How can I style fonts, colors, and padding for Text and Button in Compose?
Prefer Material theme styles for consistency. For Text: style = MaterialTheme.typography.titleLarge, color = MaterialTheme.colorScheme.onBackground. For Buttons: use ButtonDefaults.buttonColors and shape to adjust appearance. Add external spacing with Modifier.padding().
How do I preview and test a Compose UI in Android Studio?
Add @Preview composables with showBackground = true and optionally dark mode previews. Use the Compose Preview panel to view changes instantly. For UI behavior tests, use Compose UI testing APIs to find nodes and perform clicks.
Key takeaways
- Use Material 3
TextandButtonfor accessible, theme-aware UI. - Handle long text with
maxLinesandTextOverflow(including Start and Middle ellipses). - Use
BasicText+TextAutoSizewhen text must fit constrained spaces. - For links, prefer
LinkAnnotationwithText/BasicText. - Keep interactive elements at least 48 dp to meet accessibility guidelines.
Extend this tutorial
Now that you know How to create Jetpack Compose Text and Button UI, try these next steps:
- Add icons to buttons with
IconandIconButton. - Use
ScaffoldandSnackbarHostto show messages on button clicks. - Create a create simple Android UI with Compose step by step layout using
Column,Row, andBox.
Sources / Further reading
- Text in Compose (overview): https://developer.android.com/develop/ui/compose/text
- Configure text layout (maxLines, overflow): https://developer.android.com/develop/ui/compose/text/configure-layout
- TextOverflow API (Start/Middle ellipsis): https://developer.android.com/reference/kotlin/androidx/compose/ui/text/style/TextOverflow
- Links in Text with LinkAnnotation: https://developer.android.com/develop/ui/compose/text/user-interactions
- BasicText and TextAutoSize: https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/BasicText.composable
- Material 3 Buttons guide: https://developer.android.com/develop/ui/compose/components/button
- Material 3 design system for Compose: https://developer.android.com/develop/ui/compose/designsystems/material3
Conclusion
That’s it! You’ve learned the essentials of Android UI with Jetpack Compose: adding Text, creating Buttons with onClick, styling with Material 3, handling long text elegantly, and previewing your UI. Keep practicing by combining Text and Button with other Material components, and explore more patterns in this Kotlin Android Jetpack Compose tutorial series.


