Technical
Optimising Android TWAs for Foldables with Device Posture API
October 4, 2026 · 7 min read
Designing for the Next Generation of Android Hardware
The mobile hardware landscape is changing with the introduction of foldable Android devices, such as book-style folding screens and vertical clamshell models. For developers packaging progressive web apps inside a Trusted Web Activity, these form factors offer unique opportunities to build immersive layouts. However, they also break traditional assumptions about fixed mobile screen sizes.
When an application runs on a foldable device, the viewport structure changes dynamically depending on whether the physical screen is fully open, partially folded, or closed. To design web-based apps that adapt to these physical shapes, web standards introduced the Device Posture API and custom CSS environment variables. These allow web apps inside a TWA to monitor physical hinge movements and position interfaces appropriately.
Understanding Device Postures
A foldable device can exist in several distinct physical states. Rather than relying on fragile window dimensions, the Device Posture API classifies these configurations into explicit postures. The two primary postures developers must account for are the continuous posture and the folded posture.
The continuous posture represents a state where the screen is completely flat, acting as a single, uninterrupted display. In this state, normal responsive design rules apply. The folded posture occurs when the physical hinge is at an angle, creating a physical split between screen segments. This posture is commonly referred to as tabletop mode or book mode, where one half of the screen sits on a flat surface while the other half tilts upward.
Using CSS Media Queries to Detect Posture
The most straightforward method to adapt a progressive web app to folding hardware is through CSS media features. The device-posture media query allows you to write conditional styling rules that trigger only when the physical hardware enters a specific state.
@media (device-posture: folded) { body { grid-template-rows: 1fr 1fr; } }
In this example, when the device is folded, the application layout rearranges into a two-row grid, placing content neatly on the upper and lower halves of the screen. When the device returns to a flat continuous state, the media query ceases to apply, reverting the interface to its standard single-screen layout.
Accessing the Device Posture API via JavaScript
While CSS handles style transformations, complex user interactions require JavaScript state management. The Device Posture API exposes a posture object on the global navigator instance. This allows developers to detect changes programmatically and trigger internal application updates, such as pausing a video or shifting control inputs.
You can read the current posture state directly or attach an event listener to monitor transitions in real time. This ensures that your web application remains synchronised with physical hardware changes.
if (navigator.devicePosture) { console.log('Current posture: ' + navigator.devicePosture.type); navigator.devicePosture.addEventListener('change', (event) => { console.log('Updated posture to: ' + event.type); if (event.type === 'folded') { activateTabletopLayout(); } else { activateStandardLayout(); } }); }
This approach allows for dynamic rendering adjustments, such as moving video playback to the top panel and controls to the bottom panel when the user folds the device to watch content hands-free.
Designing Around the Physical Hinge
A major design challenge of foldable screens is avoiding content truncation or distortion caused by the physical hinge structure. On devices with dual screens joined by a physical seam, overlaying text or essential buttons directly over the joint ruins usability. To solve this, CSS environment variables provide the exact coordinates of physical viewport segments.
These environment variables define the boundaries of each visible screen segment, allowing you to position elements relative to the physical seam. The variables represent the width, height, and offsets of individual segments.
| Environment Variable | Description | Usage Context |
|---|---|---|
| viewport-segment-width | Calculates the horizontal dimension of an individual screen section. | Sizing left and right layout columns on book-style foldables. |
| viewport-segment-height | Calculates the vertical dimension of an individual screen section. | Sizing top and bottom panels on clamshell-style devices. |
| viewport-segment-top | Provides the top offset coordinates of a segment relative to the layout viewport. | Aligning content to avoid overlapping physical hinges. |
By utilising these environment variables, you can ensure that important interface components, such as primary action buttons or modal windows, never render directly on top of the screen fold. To target these states precisely, you can also query segment properties directly in CSS using segment-based media features.
Best Practices for Foldable UX inside TWAs
When optimising your converted progressive web app for foldable hardware platforms, follow these critical guidelines:
- Never assume constant dimensions: A foldable device can transition between different screen layouts in a split second. Avoid hardcoding viewport widths or heights in your service logic.
- Optimise form inputs: Place keyboard areas and interactive control pads on the lower screen segment when in tabletop mode, leaving the upper screen segment for viewing data or media.
- Provide fallback designs: Not all web engines support foldable APIs uniformly yet. Always design with progressive enhancement in mind, ensuring a great single-screen experience as the default baseline.
Adapting your Trusted Web Activity for foldable systems demonstrates high attention to detail and positions your application as a modern, high-quality product in the premium Google Play ecosystem.
Ready to ship your Android app?
Paste your PWA URL, get a signed APK and a Google Play ready AAB in minutes.
Build my app