Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottom Nav Bar overlaps screen content in Jetpack Compose

I have a BottomNavBar which is called inside the bottomBar of a Scaffold.
Every screen contains a LazyColumn which displays a bunch of images.
For some reason when I finish scrolling, the BottomNavBar overlaps the lower part of the items list.
How can I fix this?

enter image description here

Here the set content of MainActivity

SetContent{     Scaffold(         topBar = {             TopAppBar(                 title = { Text(text = "tartufozon") },                 actions = {                     IconButton(onClick = { Timber.d("Mail clicked") }) {                         Icon(Icons.Default.Email, contentDescription = null)                     }                 }             )         },         bottomBar = {             BottomNavBar(navController = navController)         }     ) {     BottomNavScreensController(navController = navController)     } } 
like image 909
Gianluca Veschi Avatar asked Mar 10 '21 22:03

Gianluca Veschi


People also ask

What is recomposition in Jetpack Compose?

Recomposition is the process of calling your composable functions again when inputs change. This happens when the function's inputs change. When Compose recomposes based on new inputs, it only calls the functions or lambdas that might have changed, and skips the rest.

How do I screen size in Jetpack Compose?

How do you get the device width in jetpack compose? If you want to get the size in pixels: val screenDensity = configuration. densityDpi / 160f and multiply with dp, for example val screenHeight = configuration. screenHeightDp.

Is Jetpack Compose better than XML?

Compose allows you to do more with less code compared to XML. Compose is Intuitive. This means that you just need to tell Compose what you want to show the user. Compose is compatible with all your existing code: you can call Compose code from Views and Views from Compose.


2 Answers

As per the API definition for Scaffold, your inner content (the trailing lambda you have your BottomNavScreensController in), is given a PaddingValues object that gives you the right amount of padding for your top app bar, bottom bar, etc.

Right now, you're not referencing that padding at all and hence, your content is not padded in. This is what is causing your overlap.

You can add a Box around your BottomNavScreensController to apply the padding, or pass the padding directly into your BottomNavScreensController so that each individual screen can correctly apply the padding; either solution works.

Scaffold(     topBar = {       //     },     bottomBar = {         //     }     ) { innerPadding ->         // Apply the padding globally to the whole BottomNavScreensController         Box(modifier = Modifier.padding(innerPadding)) {             BottomNavScreensController(navController = navController)         }     } } 
like image 91
ianhanniballake Avatar answered Oct 06 '22 03:10

ianhanniballake


Following ianhanniballake's answer and its comments and just to save you few minutes. The code would be something like:

Scaffold(     topBar = {       //     },     bottomBar = {         //     }     ) { innerPadding ->         Box(modifier = Modifier.padding(           PaddingValues(0.dp, 0.dp, 0.dp, innerPadding.calculateBottomPadding()) {             BottomNavScreensController(navController = navController)         }     } } 
like image 21
RobertoAllende Avatar answered Oct 06 '22 04:10

RobertoAllende