Build: Greeting Screen
This screen shows a greeting message using a composable function. It displays a simple text greeting centered on the screen.
Target UI
--------------------- | | | | | Hello, User! | | | | | ---------------------
--------------------- | | | | | Hello, User! | | | | | ---------------------
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) { // TODO: Display greeting text here } @Composable fun GreetingScreen() { Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { // TODO: Call Greeting composable here } } @Preview(showBackground = true) @Composable fun GreetingScreenPreview() { GreetingScreen() }
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.
--------------------- | | | | | Hello, User! | | | | | ---------------------
@Composable function in Android Kotlin?@Composable@Composable annotation marks functions that create UI elements in Jetpack Compose.@Composable = UI builder [OK]@Composable means UI code [OK]@Composable handles logic like networking@Composable with lifecycle methods@Composable before the function keyword.@Composable fun MyComponent() { /* UI code */ } correctly places @Composable before fun. The other options either lack the annotation entirely or place it incorrectly (after fun or without fun).@Composable before fun [OK]@Composable annotation@Composable after the function namefun @Composable
fun Greeting() {
Text(text = "Hello, Compose!")
}Text composableText composable displays a text label on the screen with the given string.Text shows text label [OK] @Composable
fun Welcome() {
Text(text = Hello World)
}Text must be in quotes. Here, "Hello World" is missing quotes.@Composable annotation, function name casing is allowed, and Text is valid inside composables.?
@Composable annotation and parameter@Composable and accept a String parameter for the name.@Composable fun Greeting(name: String) {
Text(text = "Hello, ${name}!")
} correctly uses @Composable, declares name: String, and interpolates it inside the text. The version without @Composable misses the annotation, the one without a parameter hardcodes the name, and the one missing the parameter type is invalid.@Composable annotationPass the test and this pattern moves unknown