How to create vertical dividers with Jetpack Compose? I try to use Spacer and Box to do it, but it doesn't show up at all. Here is what i tried:
Box(
modifier = Modifier
.fillMaxHeight()
.width(2.dp)
.background(color = Color.Black)
)
But that doesn't work at all. So how to do vertical divider in Jetpack Compose?
You can use the Divider
composable with the width(xx.dp)
modifier applying an intrinsic measurements to its parent container.
Something like:
Row(Modifier
.height(IntrinsicSize.Min) //intrinsic measurements
.fillMaxWidth()
.background(Color.Yellow)
) {
Text("First Text")
Divider(
color = Color.Red,
modifier = Modifier
.fillMaxHeight() //fill the max height
.width(1.dp)
)
Text("Second text")
}
As explained in the doc:
The Row composable's
minIntrinsicHeight
will be the maximumminIntrinsicHeight
of its children. TheDivider
element'sminIntrinsicHeight
is0
as it doesn't occupy space if no constraints are given; theTextField
minIntrinsicHeight
will be that of the content given a specific width. Therefore, theRow
element's height constraint will be the maxminIntrinsicHeight
of theTextField
s content. TheDivider
will then expand its height to the height constraint given by theRow
.