loloof64
09/02/2020, 5:01 PMpackage com.loloof64.simplecomposecounter
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Text
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Divider
import androidx.compose.material.Surface
import androidx.compose.material.TextButton
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.platform.setContent
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.ui.tooling.preview.Preview
import com.loloof64.simplecomposecounter.ui.SimpleComposeCounterTheme
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainScreen()
}
}
}
@Composable
fun Counter(fontSizeSp: Int) {
val buttonsBackgroundColor = Color.Green
val valueBackgroundColor = Color.Blue
val commonTextColor = Color.White
val commonTextStyle =
TextStyle(
fontSize = TextUnit.Sp(fontSizeSp),
fontWeight = FontWeight.Bold,
color = commonTextColor,
)
val buttonsTextStyle = commonTextStyle.copy(
background = buttonsBackgroundColor
)
val commonButtonModifier = Modifier.background(Color.Transparent)
val countValue = remember { mutableStateOf(0) }
Row(verticalGravity = Alignment.CenterVertically) {
TextButton(
onClick = { if (countValue.value > 0) countValue.value-- },
modifier = commonButtonModifier
) {
Text(text = "-", style = buttonsTextStyle)
}
Text(
text = "%02d".format(countValue.value),
style = commonTextStyle,
modifier = Modifier.background(valueBackgroundColor).padding(5.dp)
)
TextButton(
onClick = { if (countValue.value < 99) countValue.value++ },
modifier = commonButtonModifier
) {
Text(text = "+", style = buttonsTextStyle)
}
}
}
@Composable
fun MainScreen() {
SimpleComposeCounterTheme {
Surface(color = Color.Cyan) {
Column(modifier = Modifier.padding(5.dp)) {
Divider()
Counter(10)
Divider()
Counter(20)
Divider()
Counter(40)
Divider()
}
}
}
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
MainScreen()
}
You can see that the +/- buttons does not expand to take all their available space.Zach Klippenstein (he/him) [MOD]
09/02/2020, 5:08 PMexpand to take the size of all of its parentThis sounds like
Modifier.fillMaxSize()
, but if you want your buttons to be square i’m not sure it will actually do thatloloof64
09/02/2020, 5:45 PMZach Klippenstein (he/him) [MOD]
09/02/2020, 5:47 PMModifier.aspectRatio
?loloof64
09/02/2020, 5:47 PMModifier.background(Color.Transparent).aspectRatio(0.6f)
But it did not work eitherZach Klippenstein (he/him) [MOD]
09/02/2020, 5:53 PMloloof64
09/02/2020, 5:57 PMHalil Ozercan
09/02/2020, 7:43 PMaspectRatio
doesn't work as expected without a proper width or height, e.g. fillMaxWidth().aspectRatio(0.4f)
or
height(36.dp).aspectRatio(1f)