I have a box with a `.fillMaxWidth` modifier. In t...
# compose
n
I have a box with a
.fillMaxWidth
modifier. In that box, I have another box and I want it to be the width of the parent box divided by 7. How can I get the width of the parent Box, or that .
fillMaxWidth
value?
n
this one is pretty easy fortunatelly, fillMaxWidth() take a float that represent a fraction of the max width your "sub"box can take so you can just do:
Copy code
Box {
   Box(Modifier.fillMaxWidth(1/7f)) {
   }
}
👍 2
n
Oooh I see! lol, thanks for that!
n
I'm not 100% sure it will works so keep us updated 😄
n
hmm, I don't think it's working in my particular case. My code looks like this:
Copy code
Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(500.dp)
            .padding(20.dp)
            .background(Color.DarkGray)
    ) {
        Column() {
    repeat(7){
        Box(
            Modifier
                .padding(3.dp)
                .background(Color.Magenta, shape = RoundedCornerShape(5.dp))) {

        }
And basically the size of my squares need to be the width of the parent Box divided by 7
n
oh then you can just use Modifier.weight(1f) on your sub box
weight is used to "share" the available space with other views in the layout
if each one of your views has a weight of 1, they will all split the available space (the width of your parent) equally
sorry I forgot that it will only works in a Row or a Column
n
It is a Column though
n
yes, I just saw that you were using that, the issue will be that it will be the height that is splitted equally, not the width
n
oh damn
n
let me check
😁 1
Copy code
Box(
    modifier = Modifier
        .fillMaxWidth()
        .height(500.dp)
        .padding(20.dp)
        .background(Color.DarkGray)
) {
    Column {
        repeat(7) {
            Box(
                Modifier
                    .padding(3.dp)
                    .height(50.dp)
                    .fillMaxWidth(1/7f)
                    .background(Color.Magenta, shape = RoundedCornerShape(5.dp))
            ) {
            }
        }
    }
}
this should work
be aware that as Box as no "content", even by doing
.background(Color.Magenta, shape = RoundedCornerShape(5.dp))
your child boxes won't be displayed as you need to fix the height of each of them, or put content in it (text, image)
n
ok thanks for this. I wanted their height to be the same as their width. Is there any way for this to happen?
n
yes and this is one of my favorite feature 😄 :
Copy code
Box(
    Modifier
        .padding(3.dp)
        .fillMaxWidth(1 / 7f)
        .aspectRatio(1f)
👍 1
n
Ah brilliant, thanks!
n
(in case you don't know : order of modifiers matters)
n
It worked in any case! 👏
m
@nitrog42 any idea how to do the same with rows ? They seems to take the fraction of space left