0
0
Android Kotlinmobile~5 mins

Text composable in Android Kotlin

Choose your learning style9 modes available
Introduction

The Text composable shows words or sentences on the screen. It helps users read information in your app.

To display a greeting message like 'Hello, friend!'
To show instructions or labels for buttons and inputs
To present data like scores, names, or dates
To add titles or subtitles in your app screens
To show error messages or notifications
Syntax
Android Kotlin
Text(text = "Your text here")

The text parameter is required and shows what you want on screen.

You can customize the look with extra parameters like fontSize, color, and fontWeight.

Examples
Shows simple text on the screen.
Android Kotlin
Text(text = "Hello, World!")
Text with bigger font size for emphasis.
Android Kotlin
Text(text = "Welcome", fontSize = 24.sp)
Text in red and bold to show an error message.
Android Kotlin
Text(text = "Error", color = Color.Red, fontWeight = FontWeight.Bold)
Sample App

This composable shows a blue, medium-weight text with size 20sp that says 'Welcome to my app!'.

Android Kotlin
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp

@Composable
fun Greeting() {
    Text(
        text = "Welcome to my app!",
        fontSize = 20.sp,
        color = Color.Blue,
        fontWeight = FontWeight.Medium
    )
}
OutputSuccess
Important Notes

Use sp units for font size to respect user settings.

Text composable automatically wraps text if it is too long for the screen width.

For accessibility, Text is read by screen readers by default.

Summary

The Text composable displays words or sentences on screen.

You must provide the text parameter with the string to show.

You can style text with font size, color, and weight to make it clear and attractive.