Basic question: I want a column half width against...
# compose
t
Basic question: I want a column half width against the right side of the view/parent. The fillMaxWidth(0.5f) is perfect for the width, but there is no offsetMaxWidth and the offset(x = ) expects dp. What is the simplest way to calc the offset or have it start in the middle? Thanks!
a
It sounds like you don't necessarily want a raw offset, you want to fill the entire available width but center the content within that space while only consuming half of it:
Copy code
Modifier.fillMaxWidth() // consume all available width
  .wrapContentWidth() // wrap within the assigned max space; centers by default
  .fillMaxWidth(0.5f) // fill half of the max
other params to
wrapContentWidth
can align it within the larger space any way you'd like. (start, center, end, etc.)
c
It might be clearer if you wrap it in a row with multiple columns, each with
weight(1f)
instead of manually determining fractional widths
Copy code
Row {
    // left column content
    Column(modifier = Modifier.weight(1f)) {

    }

    // right column content
    Column(modifier = Modifier.weight(1f)) {

    }
}
p
I would just wrap it with a box and set Alignment to TopEnd:
Copy code
@Composable
fun TestBox() {
    Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.TopEnd) {
        Column(modifier = Modifier
            .fillMaxWidth(0.5f)
            .height(300.dp)) {
            Surface(modifier = Modifier.fillMaxSize(), color = Color.Red) {

            }
        }
    }
}
result:
t
Thank you all. The Row solution won. if i need it on the right, I add a column to the left. if i need it on the left, i add a column to the right. otherwise, just a single fullmax column. thanks again. note this is part of a scrollable column, so i need to position in a kind of absolute way.
389 Views