MainActivity.kt
package com.cfsuman.jetpackcompose
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
class MainActivity : AppCompatActivity() {
@ExperimentalMaterialApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
GetScaffold()
}
}
@ExperimentalMaterialApi
@Composable
fun GetScaffold(){
var favCounter = remember {mutableStateOf(0)}
Scaffold(
topBar = { TopAppBarContent(favCounter) },
content = {MainContent(favCounter)},
backgroundColor = Color(0xFFEDEAE0)
)
}
@ExperimentalMaterialApi
@Composable
fun TopAppBarContent(favCounter: MutableState<Int>) {
TopAppBar(
title = { Text(text = "Compose - Update state")},
backgroundColor = Color(0xFFC0E8D5),
actions = {
Box(modifier = Modifier
.size(36.dp)
.clip(CircleShape)
.background(Color(0xFFF0FFFF))
.padding(8.dp),
contentAlignment = Alignment.Center
) {
Text(
text = "${favCounter.value}",
fontWeight = FontWeight.Bold
)
}
Spacer(modifier = Modifier.requiredWidth(12.dp))
}
)
}
@Composable
fun MainContent(favCounter: MutableState<Int>){
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
){
Column(
modifier = Modifier
.wrapContentSize(Alignment.Center)
.padding(24.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
Text(
text = "Update state of another function variable",
style = MaterialTheme.typography.h6
)
Button(
onClick = {
favCounter.value++
}
) {
Text(text ="Update State")
}
}
}
}
@Preview
@Composable
fun ComposablePreview(){
//GetScaffold()
}
}
- jetpack compose - How to use ModalDrawer
- jetpack compose - How to use BadgeBox
- jetpack compose - TopAppBar navigation
- jetpack compose - TopAppBar actions
- jetpack compose - AndroidView click event
- jetpack compose - AndroidView modifier
- jetpack compose - How to use bottom navigation
- jetpack compose - Double click listener
- jetpack compose - Long click listener
- jetpack compose - Pass onClick event to function
- jetpack compose - How to hide system bars
- jetpack compose - Detect screen orientation change
- jetpack compose ktor - How to get api data
- jetpack compose - Icon from vector resource
- jetpack compose - IconButton from vector resource