question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BottomSheet] Padding for status bar not applied when starting expanded

See original GitHub issue

Description: Top padding for status bar not applied when starting expanded with edge-to-edge. The content view is then drawn below our transparent status bar. If you start sliding the bottom sheet the padding get set properly.

Expected behavior: Top padding should be applied even when starting expanded.

Source code: Assuming edge to edge attribute is set to true and transparent status bar:

    val bottomSheetDialog = BottomSheetDialog(this)
    // We need to set private data member edgeToEdgeEnabled to true to get full screen effect
    // That has been already fixed since alpha 02 release, this is not our issue here but was left for completeness 
    val field = BottomSheetDialog::class.java.getDeclaredField("edgeToEdgeEnabled")
    field.isAccessible = true
    field.setBoolean(bottomSheetDialog,true)
    //
    bottomSheetDialog.setContentView(myView)
    bottomSheetDialog.show()
    bottomSheetDialog.behavior.skipCollapsed = true
    bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED

To workaround that issue I had to:

    mainHandler.postDelayed( {
        bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
    },100)

Analysis ̀EdgeToEdgeCallback.setPaddingForPosition is not getting called when setting the state to expanded just after the call to show. For some reason ̀EdgeToEdgeCallback.onStateChanged is not called then. Moreover if the content of the sheet changes causing it to change its size the padding can get out of sync. Like showing top padding when none needed because sheets got smaller or not showing the padding when needed because sheet got bigger. ̀EdgeToEdgeCallback.setPaddingForPosition should also be called whenever the sheet size is changed.

Android API version: 28

Material Library version: 1.4.0-alpha02

Device: F(x)Tec Pro¹

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Slioncommented, Apr 9, 2021

Here is what I did to workaround those issues. Just calling that function whenever the bottom sheet size changes does the trick:

/**
 * To fix issues with wrong edge-to-edge bottom sheet top padding and status bar icon color…
 * …we need to call [BottomSheetDialog.EdgeToEdgeCallback.setPaddingForPosition] which is a private function from a private class.
 * See: https://github.com/material-components/material-components-android/issues/2165
 */
fun adjustBottomSheet(aDialog : BottomSheetDialog) {
    // Get our private class
    val classEdgeToEdgeCallback = Class.forName("com.google.android.material.bottomsheet.BottomSheetDialog\$EdgeToEdgeCallback")
    // Get our private method
    val methodSetPaddingForPosition: Method = classEdgeToEdgeCallback.getDeclaredMethod("setPaddingForPosition", View::class.java)
    methodSetPaddingForPosition.isAccessible = true
    // Get private field containing our EdgeToEdgeCallback instance
    val fieldEdgeToEdgeCallback = BottomSheetDialog::class.java.getDeclaredField("edgeToEdgeCallback")
    fieldEdgeToEdgeCallback.isAccessible = true
    // Get our bottom sheet view field
    val fieldBottomField = BottomSheetDialog::class.java.getDeclaredField("bottomSheet")
    fieldBottomField.isAccessible = true
    // Eventually call setPaddingForPosition from EdgeToEdgeCallback instance passing bottom sheet view as parameter
    methodSetPaddingForPosition.invoke(fieldEdgeToEdgeCallback.get(aDialog),fieldBottomField.get(aDialog))
}
0reactions
drchencommented, Feb 16, 2022

I have a fix just merged internally. Should be copied over here very soon if everything looks fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Persistent BottomSheet below ActionBar - Stack Overflow
The accepted answer works when you expand the bottom sheet to the full screen, but when collapsing it, it adds additional margin that...
Read more >
Why fitsSystemWindows doesn't work sometimes? |
fitsSystemWindows = true sets the padding to your view (if needed) to prevent your content from being obscured by the system status bar...
Read more >
Sheets: bottom - Material Design
BottomSheetDialog will also add padding to the top when the bottom sheet slides under the status bar to prevent content from being drawn...
Read more >
Flutter Beginners Guide to using the Bottom Sheet - Medium
The bottom sheet in Flutter is shown using the call showBottomSheet . Looking inside of that call we see the following line. Scaffold.of(context) ......
Read more >
Implementing Bottom Sheet Dialogs using Android Studio
A Bottom Sheet dialog is like a message box triggered by the user's actions. Companies such as Google, Facebook, and Twitter have implemented...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found