import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CounterScreen()
}
}
}
@Composable
fun CounterScreen() {
var count by remember { mutableStateOf(0) }
Column(
modifier = Modifier.fillMaxSize().padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "$count", style = MaterialTheme.typography.displayLarge)
Spacer(modifier = Modifier.height(24.dp))
Button(onClick = { count++ }) {
Text("Increase")
}
}
}
@Preview(showBackground = true)
@Composable
fun CounterScreenPreview() {
CounterScreen()
}This app uses a count variable to hold the current number. It is a mutableStateOf, which means Compose watches it for changes. When the button is clicked, count++ increases the number. Because count is state, Compose automatically redraws the Text showing the number. This shows how state drives UI updates: changing the state triggers the UI to update without extra code.