Logo

dev-resources.site

for different kinds of informations.

Fixing Rounded Navigation Bar Corner Padding in Jetpack Compose

Published at
1/2/2025
Categories
kotlin
jetpack
jetpackcompose
android
Author
houzifahabbo
Author
12 person written this
houzifahabbo
open
Fixing Rounded Navigation Bar Corner Padding in Jetpack Compose

When implementing a rounded bottom navigation bar in Jetpack Compose, you might encounter an issue where the system padding becomes visible in the corners, disrupting the clean, rounded appearance you're aiming for. Let's break down the root cause and how to resolve this common UI problem.

The Cause

In the image below, you can see unwanted edges in the corners that aren't clipped by the navigation bar. These edges are caused by the scaffold padding (innerPadding), which is used to manage the padding for the bottom bar, top bar, and system bars, ensuring UI elements don't overlap. While this padding is useful for layout adjustments, it interferes with achieving a seamless, rounded design for the bottom navigation bar.

rounded bottom navigation bar with unwanted edges in Jetpack Compose

Cases and Solutions

1. Using a LazyColumn Inside Another Container:

  • Cause: The issue often arises when using a LazyColumn inside another container, such as a Column, where the height of the LazyColumn is constrained by the parent container defined in the Scaffold. Consider the following example:
Scaffold(
    containerColor = Color(0xFFC9E4FD),
    topBar = { AppBar() },
    bottomBar = { BottomBar() }
) { innerPadding ->
    Column(
        verticalArrangement = Arrangement.spacedBy(8.dp),
        modifier = Modifier.padding(innerPadding)
    ) {
        Box(
            modifier = Modifier
                .padding(8.dp)
                .background(
                    Color.DarkGray,
                    shape = RoundedCornerShape(16.dp)
                )
                .width(200.dp)
                .height(100.dp)
        ) {
            Greeting("Android")
        }
        LazyColumn(
            contentPadding = PaddingValues(8.dp),
            verticalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            items(10) {
                Box(
                    modifier = Modifier
                        .background(
                            Color(0xFF001E30),
                            shape = RoundedCornerShape(16.dp)
                        )
                        .fillMaxWidth()
                        .height(100.dp)
                ) {
                    Greeting("Android")
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Solution: To remove the extra padding, adjust the innerPadding values:
val topPadding = innerPadding.calculateTopPadding()
var bottomPadding = innerPadding.calculateBottomPadding()
if (bottomPadding > 0.dp) bottomPadding -= 15.dp

Column(
    modifier = Modifier.padding(top = topPadding, bottom = bottomPadding)
) {
    // Your UI components here
}
Enter fullscreen mode Exit fullscreen mode

This solution reassigns the value of the bottom padding to remove the extra padding. The if condition ensures the padding value remains non-negative, preventing errors like this:

FATAL EXCEPTION: main
Process: com.example.bottombar, PID: 25016
java.lang.IllegalArgumentException: Padding must be non-negative
Enter fullscreen mode Exit fullscreen mode
  • Complete Code for the First Solution:
Scaffold(
    containerColor = Color(0xFFC9E4FD),
    topBar = { AppBar() },
    bottomBar = { BottomBar() }
) { innerPadding ->
    val topPadding = innerPadding.calculateTopPadding()
    var bottomPadding = innerPadding.calculateBottomPadding()
    if (bottomPadding > 0.dp) bottomPadding -= 10.dp

    Column(
        modifier = Modifier.padding(
           top = topPadding,
           bottom = bottomPadding
        )
    ) {
        Box(
            modifier = Modifier
                .padding(8.dp)
                .background(
                    Color.DarkGray,
                    shape = RoundedCornerShape(16.dp)
                )
                .width(200.dp)
                .height(100.dp)
        ) {
            Greeting("Android")
        }
        LazyColumn(
            contentPadding = PaddingValues(
                top = 8.dp,
                bottom = 20.dp,
                start = 8.dp,
                end = 8.dp
            ),
            verticalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            items(10) {
                Box(
                    modifier = Modifier
                        .background(
                            Color(0xFF001E30),
                            shape = RoundedCornerShape(16.dp)
                        )
                        .fillMaxWidth()
                        .height(100.dp)
                ) {
                    Greeting("Android")
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Assigning innerPadding to LazyColumn:

  • Cause: If you assign the innerPadding directly to the LazyColumn using Modifier.padding(), it can cause the same issue. Here's an example:
Scaffold(
    containerColor = Color(0xFFC9E4FD),
    topBar = { AppBar() },
    bottomBar = { BottomBar() }
) { innerPadding ->
    LazyColumn(
        verticalArrangement = Arrangement.spacedBy(8.dp),
        modifier = Modifier.padding(innerPadding)
    ) {
        items(10) {
            Box(
                modifier = Modifier
                    .background(
                        Color(0xFF001E30),
                        shape = RoundedCornerShape(16.dp)
                    )
                    .fillMaxWidth()
                    .height(100.dp)
            ) {
                Greeting("Android")
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Solution: Instead of applying innerPadding directly to the LazyColumn's modifier, assign it to the contentPadding parameter:
Scaffold(
    containerColor = Color(0xFFC9E4FD),
    topBar = { AppBar() },
    bottomBar = { BottomBar() }
) { innerPadding ->
    LazyColumn(
        contentPadding = innerPadding,
        verticalArrangement = Arrangement.spacedBy(8.dp),
        modifier = Modifier.padding(8.dp)
    ) {
        items(10) {
            Box(
                modifier = Modifier
                    .background(
                        Color(0xFF001E30),
                        shape = RoundedCornerShape(16.dp)
                    )
                    .fillMaxWidth()
                    .height(100.dp)
            ) {
                Greeting("Android")
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

By assigning innerPadding to contentPadding, this fixes the problem and ensures that the padding is handled correctly without affecting the rounded corners of your bottom navigation bar.


Final Result

rounded bottom navigation bar in Jetpack Compose

Conclusion

These two solutions address the common issue of visible system padding interfering with the rounded corners of a bottom navigation bar in Jetpack Compose. By either adjusting the padding for the whole layout or assigning innerPadding to contentPadding, you can achieve a clean, rounded appearance without the unwanted white edges.

Follow me:

If you found this post helpful, please follow me on GitHub and LinkedIn!

Share your thoughts:

Iโ€™d love to hear your opinion or any alternative solutions you think might work better. Letโ€™s discuss in the comments!

jetpackcompose Article's
30 articles in total
Favicon
Fixing Rounded Navigation Bar Corner Padding in Jetpack Compose
Favicon
Build Android App Widgets With Jetpack Glance
Favicon
Type-Safe Navigation in Jetpack Compose: Passing Custom Classes
Favicon
Cross-Platform UI Development with Jetpack Compose Multiplatform
Favicon
Implementing Live Camera OCR with Jetpack Compose
Favicon
Build a Flashlight in Jetpack Compose
Favicon
Exemplificando SOLID com Jetpack Compose - parte S
Favicon
Shimmer animations in Jetpack Compose without extra dependencies
Favicon
Getting started with splash screen in Jetpack Compose
Favicon
First steps using Material3 in a WearOS Android App
Favicon
On building a digital assistant for the rest of us (part 4)
Favicon
On building a digital assistant for the rest of us (part 3)
Favicon
๐ŸŒฑ Type-safe navigation in Jetpack Compose - Quick Guide
Favicon
On building a digital assistant for the rest of us (part 2)
Favicon
On building a digital assistant for the rest of us (part 1)
Favicon
Estado en aplicaciones Android| Jetpack Compose
Favicon
Need help with App language | Android Compose
Favicon
Mastering Jetpack Compose: Tips and Tricks for Modern Android UI Development
Favicon
How to Provide Accessibility in your Android App | Part 4: List, Link Semantics andย Testing
Favicon
Making Your Android App Accessible: Semantic Properties and Screen Orientation โ€” Part 3
Favicon
How to Provide Accessibility in Your Android App | Scaling * Text Size * Focus Order * Labeling โ€” Part 2
Favicon
How to Provide Mobile Accessibility in Your Native Android App | Guide โ€” Part 1
Favicon
Apollo GraphQL Integration in Jetpack Compose
Favicon
Accessibility Considerations with Stacked Cards Custom Layout
Favicon
Don't Lock the Screen Orientation! Handling Orientation in Compose
Favicon
Performance Optimization of LazyColumn in Jetpack Compose
Favicon
Adapt Kotlin 2.0 in Android applications
Favicon
Jetpack Compose -Difference between mutableStateOf() and derivedStateOf()
Favicon
List Formatter in Android : Android Internationalization part 1
Favicon
Enable Edge to Edge in Android Jetpack Compose (Transparent Status Bar)

Featured ones: