Mode:
Duration:
1
Coding works best on desktop or with an external keyboard.
Coding works best on desktop or with an external keyboard.
Multiple independent counters in a single view.
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MultiCounterApp()
}
}
}
@Composable
fun MultiCounterApp() {
var counters by remember { mutableStateOf(listOf(0, 0, 0)) }
Column(modifier = Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(16.dp)) {
counters.indices.forEach { i ->
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text("Counter ${i+1}: ${counters[i]}", style = MaterialTheme.typography.h5)
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
Button(onClick = { counters = counters.toMutableList().also { it[i]++ } }) { Text("+") }
Button(onClick = { counters = counters.toMutableList().also { it[i]-- } }) { Text("-") }
}
}
}
}
}Jetpack Compose is Android’s modern toolkit for building native UI using Kotlin, offering a declarative approach to designing app interfaces and simplifying UI development for Android.
Origin & Creator
Developed by Google and introduced in 2019 to modernize Android UI development with a declarative approach using Kotlin.
Industrial Note
Jetpack Compose is widely used for modern Android apps, enterprise applications, and apps that require dynamic UI updates and reactive components.