3

I have the following bottom sheet:

<androidx.compose.ui.platform.ComposeView
    android:id="@+id/observation_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:behavior_hideable="true"
    app:behavior_skipCollapsed="true"
    app:layout_insetEdge="bottom"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" />

Which is initialized as:

val bottomSheet = view.findViewById<View>(R.id.bottom_sheet)
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
bottomSheetBehavior.isFitToContents = true
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN

And I setup the compose view as:

view.bottom_sheet.setContent {
   BottomSheetContent(viewModel.content)
}

BottomSheetContent has an optional header, when shown this header changes the height of the view. However when switching between view that have or don't have the header the bottom sheet height is not being recalculated properly.

I have seen this post: Dynamically change height of BottomSheetBehavior

And while this solution will force a view resize, there is a very noticeable change in the views height a second after its shown. Not really optimal.

bottomSheetBehavior.addBottomSheetCallback(object : 
  BottomSheetBehavior.BottomSheetCallback() {
   override fun onStateChanged(bottomSheet: View, newState: Int) {
      bottomSheet.post { //workaround for the bottom sheet  height bug
         bottomSheet.requestLayout()
         bottomSheet.invalidate()
      }
   }

   override fun onSlide(bottomSheet: View, slideOffset: Float) {}
})

Looking at this post I see a reference to this being fixed in support lib >= 24.0.0, however I am using:

implementation 'com.google.android.material:material:1.4.0'

Any ideas if this is a bug in compose? A bug in bottom sheet? Or possibly something I am doing wrong when using a compose view for a bottom sheet.

NOTE: I cannot use a "compose first view" with a scaffold for my entire activity, I am stuck embedding a ComposeView in my layout for now.

lostintranslation
  • 23,756
  • 50
  • 159
  • 262
  • I am experiencing a similar problem in an old part of the app where I must use ComposeView. Did you find a solution? – seanjfarrell Apr 06 '23 at 17:27

0 Answers0