Text Composable in Android Jetpack
Text composable is used to display text on the screen.
The following is a basic example of Text composable.
Text(
text = "Hello Android"
)
Text composable displays the content (String value) given via text parameter.
Let us use it in an Android Application, and see how the text is displayed on the screen.
MainActivity.kt
package com.example.myapplication
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import com.example.myapplication.ui.theme.MyApplicationTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
Surface(
modifier = Modifier.fillMaxWidth(),
color = MaterialTheme.colorScheme.background
) {
Text(
text = "Hello Android"
)
}
}
}
}
}
Screenshot
You can also change some basic properties of the text in Text composable, by setting the respective parameters. Some of the properties are font weight, color, font size, alignment, font family, etc.