import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
@Composable
fun Greeting(name: String) {
Text(text = "Hello, $name!")
}
@Composable
fun GreetingScreen() {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Greeting(name = "User")
}
}
}
@Preview(showBackground = true)
@Composable
fun GreetingScreenPreview() {
GreetingScreen()
}We created a composable function Greeting that takes a name parameter and displays a greeting message using Text. In GreetingScreen, we used a Box with contentAlignment = Alignment.Center to center the greeting text both vertically and horizontally. We passed the string "User" to the Greeting composable to show the message "Hello, User!" on the screen.