https://kotlinlang.org logo
#compose
Title
# compose
l

loloof64

09/02/2020, 5:01 PM
Hi everyone ! What is the simpliest way to make a Text composable expend to take the size of all of its parent, and not the size of its content ?
Here my current result, in picture
Here is my attempt
Copy code
package 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.
z

Zach Klippenstein (he/him) [MOD]

09/02/2020, 5:08 PM
expand to take the size of all of its parent
This sounds like
Modifier.fillMaxSize()
, but if you want your buttons to be square i’m not sure it will actually do that
l

loloof64

09/02/2020, 5:45 PM
Thank you. Indeed, I want my buttons to be squares. I'll try it anyway 😃
z

Zach Klippenstein (he/him) [MOD]

09/02/2020, 5:47 PM
maybe try
Modifier.aspectRatio
?
l

loloof64

09/02/2020, 5:47 PM
Thanks (fillMaxSize() did not work)
I tried something like
Copy code
Modifier.background(Color.Transparent).aspectRatio(0.6f)
But it did not work either
z

Zach Klippenstein (he/him) [MOD]

09/02/2020, 5:53 PM
Maybe the background is being drawn without the aspect ratio – you might need to put the aspect ratio modifier before the background.
l

loloof64

09/02/2020, 5:57 PM
Thank you, I try it right now 😃
It did not work neither : I tried with 0.4f and 1.6f
h

Halil Ozercan

09/02/2020, 7:43 PM
aspectRatio
doesn't work as expected without a proper width or height, e.g.
fillMaxWidth().aspectRatio(0.4f)
or
height(36.dp).aspectRatio(1f)
2 Views