import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.mysecondcomposeapp.ui.theme.MySecondComposeAppTheme
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MySecondComposeAppTheme {
Surface(color = MaterialTheme.colors.background) {
MainContent()
}
}
}
}
@Composable
fun MainContent(){
Column(
Modifier
.padding(25.dp)
) {
val checkedState = remember { mutableStateOf(false)}
var message = remember { mutableStateOf("Hello!") }
var txtState = remember { mutableStateOf("0")}
Row(
Modifier.padding(10.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = message.value,
style= TextStyle(
color= Color.Red,
fontSize = 28.sp
)
)
Checkbox(
checked = checkedState.value,
onCheckedChange = { checkedState.value = it },
)
Text(
text = "Do you like coffee?",
fontWeight = FontWeight.Bold,
modifier = Modifier
.padding(start = 10.dp),
)
}
var result = "unchecked"
var textColor = Color.Red
if (checkedState.value){
result = "checked"
textColor = Color.Blue
}
Text(
text = "Checkbox is $result.",
fontSize = 28.sp,
fontStyle = FontStyle.Normal,
fontWeight = FontWeight.Normal,
fontFamily = FontFamily.SansSerif,
modifier = Modifier
.padding(20.dp),
textAlign = TextAlign.Center,
color = textColor
)
Button(onClick = {
}) {
Text(
text = "Click",
style= TextStyle(
color= Color.White,
fontSize = 20.sp
)
)
val n = txtState.value.toInt()
var total = 0
for (n in 1..n) {
total += n
}
message.value = "total: $total"
}
}
}
@Preview
@Composable
fun ComposablePreview(){
MainContent()
}
}