Preview and test your app’s edge-to-edge UI

Meghan Mehta
Android Developers
Published in
9 min readSep 4, 2024

--

This blog post is part of our series: Spotlight Week on Android 15, where we provide resources — blog posts, videos, sample code, and more — all designed to help you prepare your apps and take advantage of the latest features in Android 15. You can read more in the overview of Spotlight Week: Android 15 here, which will be updated throughout the week.

Starting in Android 15, once your app targets API 35, edge-to-edge will be enforced. Edge-to-edge means the status bar at the top and the gesture navigation bar at the bottom will be transparent by default, a look many users prefer.

How will this affect your apps?

When targeting API 35, your app’s UI might be hidden beneath system bars or display cutouts, causing layout issues. This image shows problems with the status bar, pinhole cutout, and navigation bar. To learn how to address these issues, refer to Insets handling tips for Android 15’s edge-to-edge enforcement.

Let’s see how Compose Previews can help your app achieve a seamless edge-to-edge layout and provide a high quality user experience.

Edge-to-edge support in Compose Preview

We have a number of new Compose Preview features for testing edge-to-edge that are available for you to try in the latest Canary version of Android Studio Ladybug. Note that these features are still under development, and your feedback is invaluable in making them the best they can be.

Here is a quick overview of what we will cover:

  • Improvements to showSystemUI
  • Testing your UI on specific devices
  • Testing your UI with different types of cutouts
  • Testing your UI with both types of navigation bars

Please share your feedback and suggestions using the instructions here.

Improvements to showSystemUI

In previous versions of Android Studio, you could view your screen level Compose Preview by using showSystemUi. However, it would show a system-generated status bar that looks different than it does on the emulator.

In anticipation of the Android 15 changes, we’ve been fixing this feature so that you can see a more accurate rendering of your UI with the system UI. To see the updated Preview, you can set showSystemUi to true in code through Android Studio Ladybug Feature Drop Canary 1 or higher.

@Preview(showSystemUi = true)

Note: Using showSystemUi will show your app’s UI with edge-to-edge on Compose Preview even if you are targeting SDK 34 or lower. We encourage you to consider the default edge-to-edge behavior when designing your apps.

Preview picker

You can also use Compose Preview picker to access the list of new features we added for Android 15.

Preview picker is currently experimental. To turn it on, goto Android Studio > Settings > Experimental and check the Enable @Preview picker check box.

Then, you can use the Preview picker, which can be accessed by clicking the gear icon to the left of the Preview annotation.

In the Display section, set showSystemUi to true.

Now, you can more accurately view how your app’s status and navigation bar will look with edge-to-edge implemented through both code and the Preview picker. You may also want to see how your app looks on a specific device, like a Pixel 8 Pro. Let’s see how you can achieve that through Compose Preview.

Test specific devices

You can now specify a device you want the Preview to be displayed on. This allows you to view your screen with that device’s specific cutouts and sizing.

Through the Preview picker, you can specify what device you want your Preview to be rendered on in the Device dropdown in the Hardware section.

To do this manually, add a device parameter to the Preview annotation and set it to device = “id:pixel_8_pro”. Note that autocomplete will work, so if you type device = “id:pixel” then it will fill in all of the Pixel options you can test against in your Preview.

@Preview(device = "id:pixel_8_pro", showSystemUi = true)

You can now view your app as it would be displayed on a Pixel 8 Pro through your Preview.

Note: Your navigation bar color may not be correct in the Preview and the status bar icons padding may also be different from a real device.

Here is how the same app looks on different device types:

Cutouts

Instead of testing specific devices, you may want to just test that your app’s UI renders correctly with a specific type of cutout. There are multiple types of cutouts to be aware of. In the table above, you can see that the Pixel 5 and Pixel 6 have different types of cutouts.

In Previews, we support testing for the following cutouts:

  • Corner
  • Double
  • Punch hole
  • Tall

Note: we don’t currently support the center cutout that the Pixel uses in Previews

Here’s how the UI of an app using edge-to-edge can go wrong because of a cutout:

Among other issues, this app’s cutout overlaps the list content.

This is why it is important to test how your UI reacts to different types of cutouts through your Compose Preview.

In the Preview picker, under the Hardware section, find the Cutout dropdown and specify the cutout you would like to test.

If you are doing this through code, note that you will first have to specify a device spec either through a specific device or a specific width and height in pixels.

@Preview(showSystemUi = true, device = "spec:parent=pixel_8")
@Preview(showSystemUi = true, device = "spec:width=1080px,height=2340px")

Then, within the device spec, you can also add cutout and set it equal to the type of cutout you want to test.

@Preview(showSystemUi = true, device = 
“spec:width=1080px,height=2400px,cutout=punch_hole”)

Navigation bar

For edge-to-edge, there are different UI standards for gesture navigation vs three button navigation.

Gesture navigation:

  • Transparent by default
  • Bottom offset is disabled, but you can apply insets
  • setNavigationBarColor is disabled

Three button navigation:

  • Opacity set to 80% by default
  • Bottom offset is disabled but you can apply insets
  • Color is the window background by default

The navigation bar in showSystemUi will show you the gesture navigation bar by default, but to test both types of navigation bars in your Preview, you can specify navigation using the Preview picker or through the device parameter.

To specify through the Preview picker, in the Hardware section, find the Navigation dropdown and set your Preview’s navigation bar.

If you are using code to test the navigation bars, you will need to specify a specific device or a specific width and height in pixels.

@Preview(showSystemUi = true, device = "spec:parent=pixel_8")

Then, you can add navigation and set it to buttons for three button navigation or gesture for gesture navigation.

@Preview(showSystemUi = true, device = 
"spec:parent=pixel_8,navigation=buttons")

Now that you know how to test your app’s UI edge-to-edge implementation through Compose Preview, let’s go over automated testing.

Automated testing strategies

Once you have manually tested that your screen handles edge-to-edge as expected, you should consider adding automated tests to catch future regressions.

We recommend using screenshot tests to verify your edge-to-edge implementation, as they verify the placement and dimension of your insets and the content that might be drawn behind.

You can use instrumented tests for the highest fidelity on emulators or physical devices. A single foldable emulator can cover most cases, and you can use Espresso Device to set the different screen orientations and foldable state:

To switch between navigation modes, you can use UI Automator to pass adb commands:

UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).apply {
executeShellCommand(
"cmd overlay enable-exclusive " +
"com.android.internal.systemui.navbar.gestural", // or .threebutton
)
}
An SDK 35 foldable emulator showing gesture and 3-button navigation, respectively

The system bar contains a clock and icons that change every minute, breaking screenshot tests. You can enable demo mode via adb commands to show the same system bar content every time:

Demo mode enabled, showing a fixed time and icons

Note that adb commands are not synchronized, so you might need to implement a mechanism to wait or retry until each test passes. Alternatively, if you use Compose, there is a new wrapper that can help you catch most regressions:

Testing insets with Compose

Compose 1.8.0-alpha01 ui-test includes a new DeviceConfigurationOverride for testing window insets, called DeviceConfigurationOverride.WindowInsets.

This allows for specifying an arbitrary WindowInsetsCompat to apply to the composable under test:

composeTestRule.setContent {
DeviceConfigurationOverride(
DeviceConfigurationOverride.WindowInsets(
WindowInsetsCompat.Builder()
.setInsets(
WindowInsetsCompat.Type.statusBars(),
DpRect(
left = 0.dp,
top = 64.dp,
right = 0.dp,
bottom = 0.dp,
).toAndroidXInsets(),
)
.setInsets(
WindowInsetsCompat.Type.navigationBars(),
DpRect(
left = 64.dp,
top = 0.dp,
right = 64.dp,
bottom = 64.dp,
).toInsets(),
)
.build(),
),
)
) {
Box {
content() // Your content under test
DebugVisibleWindowInsets(Modifier.fillMaxSize()) // Debug overlay (optional)
}
}
}

This can then be combined with a debug overlay for showing where the insets are:

@Composable
fun DebugVisibleWindowInsets(
modifier: Modifier = Modifier,
debugColor: Color = Color.Magenta.copy(alpha = 0.5f),
) {
Box(modifier = modifier.fillMaxSize()) {
Spacer(
modifier = Modifier
.align(Alignment.CenterStart)
.fillMaxHeight()
.windowInsetsStartWidth(WindowInsets.safeDrawing)
.windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Vertical))
.background(debugColor),
)
Spacer(
modifier = Modifier
.align(Alignment.CenterEnd)
.fillMaxHeight()
.windowInsetsEndWidth(WindowInsets.safeDrawing)
.windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Vertical))
.background(debugColor),
)
Spacer(
modifier = Modifier
.align(Alignment.TopCenter)
.fillMaxWidth()
.windowInsetsTopHeight(WindowInsets.safeDrawing)
.background(debugColor),
)
Spacer(
modifier = Modifier
.align(Alignment.BottomCenter)
.fillMaxWidth()
.windowInsetsBottomHeight(WindowInsets.safeDrawing)
.background(debugColor),
)
}
}

Putting both together, a screenshot test can visually show where the insets are, and reveal if there is content that would be obscured by the insets, like the snackbar is below:

A screenshot test for the Now In Android app, identifying an issue with the snackbar

For an example of this in action, check out this Now in Android PR, which adds screenshot tests with applying insets: https://github.com/android/nowinandroid/pull/1498/

QA Testing

Your app might decide to create a QA team to test every screen, or at least your most important screens.

There are three approaches to assist your QA team in seeing the impacts of the edge-to-edge enforcement:

  1. Distribute APKs that are targeting SDK 35 on Android 15 devices or emulators.
  2. OR, enable the ENFORCE_EDGE_TO_EDGE flag in the App Compatibility Change Developer Option on an Android 15 device without having to target SDK 35.
  3. OR, call enableEdgeToEdge on each Activity to simulate the Android 15 platform enforcement without having to target SDK 35 and without needing an Android 15 device.

Summary

Apps targeting API35 will be edge-to-edge by default in order to give your users a more satisfying and high quality experience. You can test your UI, including cutouts and navigation bars, using Compose Preview in the Canary version of Android Studio Ladybug, and in automating testing with the new DeviceConfigurationOverride. Please be sure to leave us any feedback using these instructions.

Additional Resources

The code snippets in this blog have the following license:

//Copyright 2024 Google LLC. SPDX-License-Identifier: Apache-2.0

--

--