Layouts
Overview
A user interface has to fit on many screens: a large monitor, a small laptop, a phone, or a window that someone drags to a new size. A layout is a set of rules that decides where each control goes and how big it is. Every form you have built so far used a layout without you writing one. In this tutorial, you will give part of a form its own layout, so that buttons wrap onto new lines or sit in a grid. You will also lay out a whole window yourself, with sizes in pixels and percentages that change when the window does.
Goals
You have the following goals for this tutorial:
By the end of this lesson, you can
- Explain what a layout and a container are, and what a form's layout does for you
- Give a
Groupingon a page its own layout - Arrange a row of controls that wraps with a
FlowLayout, and a grid of controls with aGridLayout - Position and size controls with pixels and with percentages in a
ManualLayout - Use
LayoutPropertiesto describe how a control is sized, including a different arrangement for narrow windows - Explain what resizing a window can and can't tell you about a phone or a desktop screen
Warm up
Open a website you use often. Make the browser window wide, and then slowly make it narrow. What stays the same size? What gets smaller? What moves to a new place, or disappears into a menu? Why do you think the designers made those choices?
Vocabulary
You will be learning about the following vocabulary words:
| Term | Definition |
|---|---|
| Layout | A set of rules that decides where each item in a container goes and how big it is. |
| Container | An item that holds other items, such as a page, a grouping, or a game window. |
| Pixel | One dot on the screen. A size in pixels stays the same, no matter how big the window is. |
| Percentage | A size or position given as a part of the container, from 0.0 (none of it) to 1.0 (all of it). |
| Origin | The point on a control that is placed at its x and y position. An origin of 0.5, 0.5 is the control's center. |
| Layout mode | A rule for how a control works out its width or height, such as STANDARD, FILL or FIT_CONTENTS. |
| LayoutProperties | An object that stores how one control should be positioned and sized. |
| Grid | An arrangement of equal-sized cells in rows and columns. |
| Span | When one control covers more than one cell of a grid. |
| Responsive design | Designing an interface so that it changes its arrangement to fit the screen or window it is shown in. |
New Code to Learn
You will be using the following new pieces of code:
| Quorum Code | Code Example | Explanation |
|---|---|---|
| SetLayout(Layout layout) | toolbar:SetLayout(flow) | Sets the layout that arranges the items inside a grouping, a Control or the game window. |
| FlowLayout | FlowLayout flow | A layout that places items left to right, starting a new line when one is full. |
| GridLayout | GridLayout grid | A layout that places items in the cells of a grid. |
| ManualLayout | ManualLayout layout | A layout that places each item where its own pixel and percentage values say. |
| SetContainerGridWidth(integer columns) | pad:SetContainerGridWidth(3) | Sets how many columns a container's grid has. |
| SetContainerGridHeight(integer rows) | pad:SetContainerGridHeight(4) | Sets how many rows a container's grid has. |
| SetGridPosition(integer x, integer y) | key:SetGridPosition(0, 3) | Sets the column and row of a control in a grid. Row 0 is the bottom row. |
| SetGridWidth(integer columns) | key:SetGridWidth(3) | Makes a control span this many columns. |
| GetDefaultLayoutProperties() | LayoutProperties properties = key:GetDefaultLayoutProperties() | Gets the LayoutProperties object that the control uses by default. |
| SetHorizontalLayoutMode(integer mode) | properties:SetHorizontalLayoutMode(properties:FILL) | Chooses how the width is worked out. |
| SetPercentageX(number x) | center:SetPercentageX(0.5) | Places the control a part of the way across its container. 0.5 is halfway. |
| SetPercentageOriginX(number x) | center:SetPercentageOriginX(0.5) | Chooses which point of the control is placed at its x position. 0.5 is its center. |
| SetPixelWidth(number width) | center:SetPixelWidth(200) | Sets a width in pixels that does not change with the window. |
| SetPercentageWidth(number width) | sidebar:SetPercentageWidth(0.25) | Sets the width as a part of the container's width. |
| SetMinimumWidth(number width) | wide:SetMinimumWidth(150) | Stops the control from getting narrower than this many pixels. |
| AddLayoutProperties(LayoutProperties properties) | sidebar:AddLayoutProperties(narrow) | Gives the control another set of properties to choose from. |
| SetMaximumContainerWidth(number maximum) | narrow:SetMaximumContainerWidth(600) | Makes these properties apply only when the container is at most this many pixels wide. |
| SetLeftMargin(number margin) | button:SetLeftMargin(5) | Adds empty space to the left of a control in a layout. |
CSTA Standards
This tutorial covers the following 2026 CSTA standards:
For teachers · 2026 CSTA K–12 Computer Science Standards
Middle School · Programming
Develop a program utilizing inclusive collaboration practices.
Also in 1 other lesson
High School · Programming
Collaborate on a programming project using a defined workflow that includes design documentation and clear task roles.
Also in 1 other lesson
High School · Systems & Security
Demonstrate the capabilities and limitations of a physical or simulated computing device to address a task or problem.
Also in 1 other lesson
High School · Programming
Evaluate a computing technology's alignment with design specifications and responsible design values, including its correctness, effectiveness, and user experience.
Also in 7 other lessons
Specialty I · Software Development
Refine a user interface design using accessibility and responsive design tools.
Also in 13 other lessons
Specialty II · Software Development
Create a user-friendly, accessible, and responsively designed interface.
Also in 1 other lesson
Explore
What is a layout?
Every control lives inside a container. A page is a container, and so
is a grouping, a game window, or any Control that you add other items to.
A container can have a layout, which is an object that positions and
sizes the items inside it. When the window changes size, the layout
arranges everything again.
A form page already has a layout. It puts each control in its own row, from top to bottom, centers the rows, and makes them 80% as wide as the page. It pairs a label with the field after it, and it puts the controls in a grouping side by side. That is why your forms have been tidy without any layout code, and why they still work when the window is narrow.
Sometimes you want a different arrangement for one part of a page, such as
a toolbar or a number pad. Quorum's library has many layout classes, but
most are built for one kind of control. For example, ButtonLayout
arranges the text and icon inside a button. Three layouts are meant for
your own programs:
- FlowLayout: items are placed in a row from left to right, starting at the top left. When a row is full, the next item starts a new row.
- GridLayout: items are placed in the cells of a grid of rows and columns.
- ManualLayout: you say exactly where each item goes and how big it is, in pixels, in percentages, or in both.
Pixels and percentages
Positions in Quorum are measured from the bottom left corner of the container. Moving right makes x bigger, and moving up makes y bigger.
You can give a position or a size in two ways:
- Pixels stay the same. A button 200 pixels wide is 200 pixels wide in a small window and in a large one.
- Percentages are a part of the container, written from 0.0 to 1.0. A button with a percentage width of 0.5 is always half as wide as its container.
You can also use both at once. The layout adds them together. For example, a percentage y of 1.0 and a pixel y of -70 means "70 pixels below the top," however tall the container is.
Layout modes
A layout mode tells a control how to work out its width or height. There are five modes:
- STANDARD: the size is a percentage of the container plus a number of pixels. Use this when you want to set the size yourself.
- FILL: the control fills the space that is left in the container.
- FIT_CONTENTS: the control is just big enough to hold what is inside it. A button uses this for its width by default.
- FIT_FONT: the height fits one line of the control's font. A button uses this for its height by default. It only works for height.
- MAINTAIN_ASPECT_RATIO: one side is worked out from the other, so the shape stays the same. Only one side can use this mode at a time.
A FlowLayout and a GridLayout read the modes to decide how big each
control should be. A ManualLayout doesn't: it uses the pixel and
percentage sizes you set.
LayoutProperties
Every control keeps its layout information in a LayoutProperties object:
its position, size, margins, grid cell and layout modes. Actions like
SetPixelWidth on a button are shortcuts that change the button's default
LayoutProperties. You can also get that object with
GetDefaultLayoutProperties and change it yourself. Some settings, such as
a minimum or maximum size, are only available this way.
The layout modes are numbers stored in LayoutProperties, so it is the
easiest place to set them:
LayoutProperties properties = key:GetDefaultLayoutProperties()
properties:SetHorizontalLayoutMode(properties:FILL)
A grouping with its own layout
In the Pages tutorial, you added groupings to a
page with AddGrouping. A grouping can have its own layout:
SetLayout gives it one. The page still places the grouping as one row, but
the grouping's layout arranges what is inside it.
The program below makes the start of a phone app with two groupings: a
toolbar that uses a FlowLayout, and a number pad that uses a
GridLayout. Replace Main.quorum with this program:
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Forms.Grouping
use Libraries.Interface.Controls.Button
use Libraries.Interface.Layouts.FlowLayout
use Libraries.Interface.Layouts.GridLayout
use Libraries.Interface.Layouts.LayoutProperties
use Libraries.Game.DesktopConfiguration
class Main
action Main
Form form
DesktopConfiguration config = form:GetDesktopConfiguration()
config:resizable = true
Page page = form:GetMainPage()
page:SetTitle("Phone")
// A row of buttons that wraps onto a new line when it runs out of room.
Grouping toolbar = page:AddGrouping("Toolbar")
FlowLayout flow
toolbar:SetLayout(flow)
AddToolButton(toolbar, "Contacts")
AddToolButton(toolbar, "Recent")
AddToolButton(toolbar, "Favorites")
AddToolButton(toolbar, "Voicemail")
AddToolButton(toolbar, "Settings")
AddToolButton(toolbar, "Help")
// A number pad with 3 columns and 4 rows.
Grouping pad = page:AddGrouping("Number pad")
GridLayout grid
pad:SetLayout(grid)
pad:SetContainerGridWidth(3)
pad:SetContainerGridHeight(4)
LayoutProperties padProperties = pad:GetDefaultLayoutProperties()
padProperties:SetVerticalLayoutMode(padProperties:STANDARD)
pad:SetPixelHeight(240)
// Keys 1 to 9, in reading order: row 3 is the top row.
integer digit = 1
integer row = 3
repeat while row >= 1
integer column = 0
repeat while column < 3
AddKey(pad, "" + digit, column, row, 1)
digit = digit + 1
column = column + 1
end
row = row - 1
end
AddKey(pad, "0", 0, 0, 3)
page:AddButton("Call")
form:Display()
end
action AddToolButton(Grouping toolbar, text name)
Button button
button:SetName(name)
button:SetDescription("Opens the " + name + " screen.")
button:SetLeftMargin(5)
button:SetRightMargin(5)
button:SetTopMargin(5)
button:SetBottomMargin(5)
toolbar:Add(button)
end
action AddKey(Grouping pad, text name, integer column, integer row, integer columnsWide)
Button key
key:SetName(name)
key:SetDescription("Number key")
LayoutProperties properties = key:GetDefaultLayoutProperties()
properties:SetHorizontalLayoutMode(properties:FILL)
properties:SetVerticalLayoutMode(properties:FILL)
key:SetLeftMargin(4)
key:SetRightMargin(4)
key:SetTopMargin(4)
key:SetBottomMargin(4)
key:SetGridPosition(column, row)
key:SetGridWidth(columnsWide)
pad:Add(key)
end
end
The first lines make the window resizable, so you can try it. Run the
program and drag the edge of the window. To resize it with the keyboard on
Windows, press Alt+Space, then S, then the arrow keys, and Enter when
it is the size you want. On any computer, you can also choose the size in
the code: add config:width = 360 just after config:resizable = true and
run the program again. Try a few widths, such as 360 and 1000.
The toolbar: a FlowLayout
The toolbar's FlowLayout puts the first button at the top left of the
grouping and each next button to its right. When the next button doesn't
fit in the row, it starts a new row underneath. Make the window narrow, and
the buttons that no longer fit move to a second row. A grouping is as tall
as what is inside it, so the toolbar grows to hold the new row, and the
number pad moves down.
Each button is as wide as its text, because a button's horizontal layout
mode is FIT_CONTENTS by default. The margins add 5 pixels of space around
each button so they don't touch.
A FlowLayout decides where each control goes, so it ignores the x and y
values you set on the controls inside it. It does use their sizes, layout
modes and margins. It also has two options:
flow:SetSingleLine(true)keeps every control on one row, even when the row is too long.flow:SetVerticalCentering(true)centers the controls in each row from top to bottom, which helps when they have different heights.
The number pad: a GridLayout
A GridLayout divides its container into cells of equal size. You tell the
container how many columns and rows it has, and you tell each control which
cell it goes in. Columns are counted from the left, starting at 0. Rows are
counted from the bottom, starting at 0, because positions in Quorum go
up from the bottom.
Some things to notice:
- The grid size belongs to the container.
SetContainerGridWidth(3)andSetContainerGridHeight(4)give the pad 3 columns and 4 rows. Always set both. - The pad needs a height of its own. A grouping's height normally fits
what is inside it, but a grid's cells get their size from the
container, so the two would wait on each other. Setting the pad's
vertical layout mode to
STANDARDand its height to 240 pixels breaks the tie. Without theSTANDARDline, the program stops with a message that theGridLayoutdoes not support the layout mode. - The top row is row 3. The loop starts at row 3 and counts down, so the keys 1, 2 and 3 end up at the top, like on a phone.
- FILL makes each key fill its cell. With both layout modes set to
FILL, each key fills its whole cell, minus its margins. - The 0 key spans three cells.
SetGridWidth(3)makes it cover columns 0, 1 and 2.SetGridHeightdoes the same for rows.
The Tab order of a grouping
A layout only changes where controls appear on the screen. It doesn't change the order in which the Tab key moves between them. On a form, the Tab order follows the order you add the controls: the toolbar's buttons from Contacts to Help, then the keys, then Call.
That is why AddKey is called in reading order: 1, 2, 3, then 4, 5, 6, and
so on, ending with 0. If the loop added the bottom row first, the pad would
look the same, but Tab would visit 7, 8 and 9 before 1, 2 and 3. Always
add controls in the order a person reads them, usually left to right and
top to bottom.
When a key has the focus, a screen reader reads its name, its description and that it is a button. Press Space or Enter to press the key.
A whole window: a ManualLayout
A form arranges a whole page for you. Sometimes, such as in a game, you
want to place everything in the window yourself. For that, a program uses
the Game class and gives the window a ManualLayout. Each control says
where it goes and how big it is.
The following program makes a window that you can resize. Each control uses a different rule:
- Center stays in the middle of the window.
- Corner is always 200 by 50 pixels, 20 pixels from the bottom left corner.
- Sidebar, a blue panel, takes the right quarter of a wide window. In a window 600 pixels wide or less, it moves to a strip across the top.
Replace Main.quorum with this game:
use Libraries.Game.Game
use Libraries.Game.DesktopConfiguration
use Libraries.Game.Graphics.Color
use Libraries.Interface.Controls.Button
use Libraries.Interface.Controls.Icon
use Libraries.Interface.Layouts.ManualLayout
use Libraries.Interface.Layouts.LayoutProperties
class Main is Game
action Main
DesktopConfiguration config = GetDesktopConfiguration()
config:resizable = true
config:title = "Manual Layout"
StartGame()
end
action CreateGame
ManualLayout layout
SetLayout(layout)
// Center: a fixed size, placed by percentage, centered on its position.
Button center
center:SetName("Center")
center:SetDescription("This button stays in the middle of the window.")
center:SetPixelWidth(200)
center:SetPixelHeight(60)
center:SetPercentageX(0.5)
center:SetPercentageY(0.5)
center:SetPercentageOriginX(0.5)
center:SetPercentageOriginY(0.5)
Add(center)
// Corner: everything in pixels.
Button corner
corner:SetName("Corner")
corner:SetDescription("This button stays near the bottom left corner.")
corner:SetPixelWidth(200)
corner:SetPixelHeight(50)
corner:SetPixelX(20)
corner:SetPixelY(20)
Add(corner)
// A game has no Tab order of its own, so link the two buttons.
center:SetNextFocus(corner)
center:SetPreviousFocus(corner)
corner:SetNextFocus(center)
corner:SetPreviousFocus(center)
center:Focus()
// Sidebar: the right quarter of a wide window, at least 150 pixels wide.
Color color
Icon sidebar
sidebar:LoadFilledRectangle(10, 10, color:Blue())
sidebar:SetName("Sidebar")
sidebar:SetPercentageX(0.75)
sidebar:SetPercentageY(0)
sidebar:SetPercentageWidth(0.25)
sidebar:SetPercentageHeight(1.0)
LayoutProperties wide = sidebar:GetDefaultLayoutProperties()
wide:SetMinimumWidth(150)
// In a window 600 pixels wide or less: a strip across the top.
LayoutProperties narrow
narrow:SetMaximumContainerWidth(600)
narrow:SetPercentageX(0)
narrow:SetPercentageY(0.85)
narrow:SetPercentageWidth(1.0)
narrow:SetPercentageHeight(0.15)
sidebar:AddLayoutProperties(narrow)
Add(sidebar)
end
action Update(number seconds)
end
end
Run the program and drag the edge of the window, or use the keys or
config:width from the Phone form. The Center button stays in
the middle, the Corner button does not change at all, and the sidebar
grows and shrinks with the window until the window is narrow enough for
the sidebar to jump to the top.
A few details in this program are worth examining:
- The origin. By default, a control's position is its bottom left
corner.
SetPercentageOriginX(0.5)andSetPercentageOriginY(0.5)move that point to the center of the button, so a position of 0.5, 0.5 puts the center of the button at the center of the window. - A minimum size.
GetDefaultLayoutPropertiesreturns the sidebar's default properties, the ones theSetPercentageactions above changed.SetMinimumWidth(150)stops the sidebar from getting narrower than 150 pixels.SetMaximumWidthdoes the opposite. - A second set of properties.
AddLayoutPropertiesgives the sidebar anotherLayoutPropertiesobject to choose from.SetMaximumContainerWidth(600)says it is only for containers 600 pixels wide or less. Each time the window is laid out, the sidebar uses thenarrowproperties if they fit, and its default properties if not. Changing the arrangement to fit the window like this is called responsive design. - A Tab order of your own. A form sets the Tab order for you, but a
game doesn't.
SetNextFocusandSetPreviousFocuslink the two buttons so Tab and Shift+Tab move between them, andFocusgives the Center button the focus at the start. The Focus tutorial explains these actions in more detail. - The sidebar is only a picture. A screen reader skips it. An
Iconis not announced unless you give it an accessibility code, which the Accessibility tutorial explains. The buttons are read by their names, followed by their descriptions.
Standing in for a phone
Programs run on screens of many sizes, and you may not have every device to test on. Resizing a window is a simulation: the window stands in for a smaller or a larger screen. It can tell you what your layout does when there is less room: which controls move, shrink, overlap or disappear, and whether a narrow arrangement, like the sidebar's, takes over when it should.
A simulation also has limits. A resized window on a computer is still used with a mouse and a keyboard. It can't tell you whether a button is big enough to tap with a finger, whether an on-screen keyboard covers your text field, or how the program feels in someone's hand. When a resized window says your layout works, treat that as a good first test, not the last one.
Engage
In this activity, you work in a team of three to design and build a calculator on a form, and then test it in a window sized like a phone and like a desktop screen. The goal is to follow a team workflow with a design document and clear roles, to build a layout with a grouping that has its own layout, and to find out what a resized window can and can't tell you.
Each person takes one role: the planner writes the design document, the builder writes the code, and the tester tests it. In a team of two, the planner also tests. On your next project, switch roles so that everyone gets a turn at each one. Work in this order:
- Design (planner, with the team). The calculator needs a text field at the top for the number being typed, a grid of number and operator keys, and a wide "equals" key. The planner sketches the keypad twice, once for a wide window and once for a narrow one, and labels the column and row of every key; remember that row 0 is at the bottom. The planner also writes the layout out as a list in words, so every teammate can review it, including one who uses a screen reader. Each teammate suggests at least one change before the team agrees on the design.
- Build (builder). The builder builds it with a
GridLayoutinside a grouping, adding the keys in reading order so Tab follows the rows from top to bottom. The builder writes down any place where the code had to differ from the design, and why. - Test (tester). Using only the keyboard, the tester checks that every
key can be reached with Tab and Shift+Tab and pressed with Space.
Then the tester drags the window, or uses the keys or
config:widthfrom Explore, to make it as narrow as a phone held upright, and then as wide as a desktop screen, and writes down anything that overlaps, disappears or becomes too small to read. - Review (whole team). Compare the test results with the design document. Agree on one change, make it, and update the document.
Finish by writing two things the resized window showed you about a phone, and two things it couldn't show you.
Wrap up
Think about an app on your phone that you also use on a computer. How does its layout change between the two? Which choices do you think were made for the size of the screen, and which were made for how people hold or use the device? Which of the three layouts would you pick for a game's inventory screen, and why? In your team, which role was the hardest, and how did the design document help the builder and the tester?
In the next tutorial, Input Sets, you will choose which keys and mouse buttons run your program's behaviors.