Edge-to-Edge Insets in Android Studio Using Jetpack Compose

Charles Raj Iruthayaraj
3 min readAug 7, 2024

--

Hello Buddy’s

In this blog post, we’ll explore how to implement edge-to-edge insets in Android Studio using Jetpack Compose. This feature allows you to utilize the entire screen space, including the areas under the system status bar and navigation bar, for your app’s design. This is a handy update in Jetpack Compose, enabling more immersive user experiences.

Introduction to Edge-to-Edge Insets

Jetpack Compose’s latest updates bring the capability to create layouts that occupy the full screen, including the system bars. Typically, developers adjust the colors of the system navigation buttons and content, but with this feature, you can extend your design to these areas without compromising usability.

Setting Up Jetpack Compose

To get started, ensure your project is set up for Jetpack Compose. You need to create a Jetpack Compose navigation and enable the necessary configurations in your project settings.

class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge() // to make the screen edge to edge
setContent {
Column(
modifier = Modifier
.fillMaxSize()
.background(color = Color.Green)
,
verticalArrangement = Arrangement.SpaceBetween
) {
Text(text = "Top Text")
TextField(value = "", onValueChange = {}, )
Text(text = "Bottom Text Text")
}
}
}
}
WindowCompat.setDecorFitsSystemWindows(window, false)

This line of code instructs the Android application to display your app behind the system bars, allowing your design to use the entire screen.

Applying Edge-to-Edge Insets

Once your project is configured, you can apply edge-to-edge insets to your layout. This involves creating a composable function that includes the necessary insets.

    override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
Column(
modifier = Modifier
.fillMaxSize()
.background(color = Color.Green)
.windowInsetsPadding(WindowInsets.safeContent)
,
verticalArrangement = Arrangement.SpaceBetween
) {
Text(text = "Top Text")
TextField(value = "", onValueChange = {}, )
Text(text = "Bottom Text Text")

}

}
}

Using `.windowInsetsPadding(WindowInsets.safeContent)`, your layout can extend under the status and navigation bars, creating a full-screen experience.

Safe Content and Gestures

Jetpack Compose provides different options to handle safe content and gestures.

  • Safe Content: Adds padding around the content to avoid overlapping with the system bars.
  • Safe Gestures: Ensures that gesture-sensitive areas, like swipe gestures, are not obstructed.
Modifier.safeContentPadding()
Modifier.safeGesturePadding()

These modifiers ensure that your layout components do not interfere with important system gestures and content areas.

Hiding System Bars

In some cases, you might want to hide the system bars to provide an even more immersive experience. This can be done using the WindowCompat library.

WindowCompat.getInsetsController(window, decorView).apply {
hide(WindowInsets.Type.statusBars())
hide(WindowInsets.Type.navigationBars())
}

By hiding the system bars, your app can use the entire screen space, making it ideal for media players, full-screen images, and other immersive content.

Practical Use Cases and Recommendations

While implementing edge-to-edge layouts, it’s crucial to design without cutting off important parts of your UI. Here are a few recommendations:

  • Safe Areas: Always use safe area insets to ensure that critical UI elements are not obscured.
  • Specific Insets: You can apply specific insets for different parts of the screen, like the status bar or navigation bar, to customize the layout further.
Modifier.padding(WindowInsets.systemBars.only(WindowInsetsSides.Top))

Using these techniques, you can create beautiful, edge-to-edge layouts that enhance the user experience by utilizing the full screen effectively.

Conclusion

Edge-to-edge insets in Jetpack Compose offer a significant advantage in modern Android app design. By extending your layouts to the system bars and handling safe areas and gestures appropriately, you can create more immersive and engaging apps.

--

--

Charles Raj Iruthayaraj

Experienced software developer with hands-on experience in Android Application and iOS development.