Using anchors can be efficient enough to handle multiple resolutions. But as the UI gets more complex, they become difficult to use.
A way we can handle more complex and advanced layouts is by making use of Containers.
Container Layout
When a Container node is used, any Control nodes that are a child give up their ability to position themselves. The parent container will handle all of these nodes position and sizes. Manually changing the child nodes position or size will either be ignored or invalidated the next time the parent is resized.
When a container is resizes, all of its children are repositioned and resized as well:

Size Flags
When adding a control node to a container, the way a container treats a child depends mainly on their size flags. These can be set on each control node by using the horizontalSizing and verticalSizing properties.
hBoxContainer {
button("Button") {
horizontalSizing = SizeFlag.FILL or SizeFlag.EXPAND
}
button("Button 2") {
horizontalSizing = SizeFlag.FILL or SizeFlag.EXPAND
}
}
Or use the shortcuts from Size and Anchors:
hBoxContainer {
button("Button") { expandWidth() }
button("Button 2") { expandWidth() }
}
Size flags are independent of vertical and horizontal sizing and not all containers make use of them:
- Fill: Ensures the control fills the designated area within the container. No matter if a control expands or not, it will only fill the designated area when this is toggled (control nodes use this by default)
- Expand: Attempts to use as much space as possible in the parent container. Controls that don’t expand will be pushed away by those that do. Between expanding controls, the amount of space they take from each other is determined by the stretch ratio.
- Shrink Center: When expanding (and not filling), try to remain at the center of the expanded area. By default, it remains at top or left.
- Stretch Ratio: A ratio of how much expanded controls take up the available space in relation to each other. A control with a stretch ratio of
2will take up twice as much available space with a control with1.
Container Types
There are several out of the box container types:
Box Containers
HBox
Arranges child controls horizontally while expanding vertically (via HBoxContainer).

hBoxContainer {
button("Button") { expandWidth() }
button("Button 2") { expandWidth() }
button("Button 3") { expandWidth() }
}
We can also use row instead which is the same as hBoxContainer.
row {
button("Button") { expandWidth() }
button("Button 2") { expandWidth() }
button("Button 3") { expandWidth() }
}
VBox
Arranges child controls vertically while expanding horizontally (via VBoxContainer)

vBoxContainer {
button("Button") { expandHeight() }
button("Button 2") { expandHeight() }
button("Button 3") { expandHeight() }
}
We can also use column instead which does the same as vBoxContainer.
column {
button("Button") { expandHeight() }
button("Button 2") { expandHeight() }
button("Button 3") { expandHeight() }
}
Separation and Alignment
Set the space between children and how they are aligned:
column(separation = 10, align = AlignMode.CENTER) {
button("Play")
button("Quit")
}
To size or align a single child, use the weight and align modifiers.
Spacer
Use spacer() to push siblings apart in a row or column:
row {
label("Score")
spacer()
button("Pause")
}
Center Container
Arranges child controls directly in the center (via CenterContainer).

centerContainer {
fillParent()
button("Button")
}
Padded Container
Arranges child controls that are expanded toward the bounds (via PaddedContainer) with an additional padding that will be added to the margins which can be configured by the theme.

paddedContainer(padding = 10) {
button("Button")
}
Or use the shorter padded, with the same padding on every side or separate horizontal and vertical padding:
padded(8) {
button("Button")
}
padded(horizontal = 16, vertical = 8) {
button("Button")
}
Panel Container
A container that renders a Drawable and expands its children to cover the whole area (via PanelContainer).

panelContainer {
width = 400f
height = 200f
label("my label") {
verticalAlign = VAlign.CENTER
horizontalAlign = HAlign.CENTER
}
}
Stack Container
Layers child controls on top of each other without drawing anything itself (via StackContainer), similar to Compose’s Box. Each child fills the stack by default and later children are drawn on top. Its minimum size is the largest child’s minimum size.
stack(Modifier.minSize(0f, 56f)) {
ninePatchRect { texture = slice }
label("Top left", modifier = Modifier.align(HAlign.LEFT, VAlign.TOP).padding(4))
label("Bottom right", modifier = Modifier.align(HAlign.RIGHT, VAlign.BOTTOM).padding(4))
}
stackContainer is the same as stack. To align a single child, use the stack scope modifiers.
Scroll Container
A container that will automatically create a ScrollBar child (HScrollBar or VScrollBar or both) when needed. (via ScrollContainer).

scrollContainer {
minHeight = 100f
vBoxContainer {
repeat(20) {
label {
text =
"I am really super duper long and awesome label ${it + 1}"
}
}
}
}
Children keep their minimum size. To stretch one to the visible width so only its height scrolls, use the scroll scope modifiers:
scrollContainer {
column(modifier = Modifier.fillViewportWidth()) { /* ... */ }
}
Custom Container
It is also possible to create our own container. All we have to do is extend the Container class and implement logic on sorting the child controls and calculating size. Here is an example of implementing a container that centers its children:
class MyCustomContainer : Container() {
override fun onSortChildren() {
nodes.forEach {
if (it is Control && it.enabled) {
val newX = floor((width - it.combinedMinWidth) * 0.5f)
val newY = floor((height - it.combinedMinHeight) * 0.5f)
fitChild(it, newX, newY, it.combinedMinWidth, it.combinedMinHeight)
}
}
}
override fun calculateMinSize() {
if (!minSizeInvalid) return
var maxWidth = 0f
var maxHeight = 0f
nodes.forEach {
if (it is Control && it.enabled) {
if (it.combinedMinWidth > maxWidth) {
maxWidth = it.combinedMinWidth
}
if (it.combinedMinHeight > maxHeight) {
maxHeight = it.combinedMinHeight
}
}
}
_internalMinWidth = maxWidth
_internalMinHeight = maxHeight
minSizeInvalid = false
}
}