Build: Simple Button Screen
A screen with a button that shows a message when clicked.
Target UI
--------------------- | | | [Click Me] | | | ---------------------
--------------------- | | | [Click Me] | | | ---------------------
import android.os.Bundle import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { Box( modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center ) { // TODO: Add Button composable here } } } }
import android.os.Bundle import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { Box( modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center ) { Button(onClick = { Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show() }) { Text(text = "Click Me") } } } } }
We use a Box to center the button on the screen by setting contentAlignment = Alignment.Center. Inside the Box, we add a Button composable with the text 'Click Me'. The onClick lambda shows a Toast message 'Button clicked!' using Toast.makeText. This gives immediate feedback when the user taps the button.
--------------------- | | | [Click Me] | | | ---------------------
Button composable in Android Jetpack Compose?Button(onClick = { println("Tapped") }) {
Text("Press Here")
}Button(onClick = println("Clicked")) {
Text("Tap")
}
What is the error?enabled = false after first click correctly uses state to disable button after click. Others ignore state or misuse enabled.Pass the test and this pattern moves unknown