1

I have countered a situation that needs to implement a scrollable modal bottom sheet. Basically, it will show half off the screen at first, then when scrolling up, it will stretch to 90% of the screen. If keep swiping up, it will scroll the content inside bottom sheet.

What I have tried

layout.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/design_bottom_sheet"
        style="@style/RootLayout"
        android:background="@drawable/rounded_shape"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <...Content.../>

        </android.core.widget.NestedScrollView>
    </FrameLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout

Bottom Sheet Dialog class

I followed the answer here accepted answer

Result

It only shows half of the screen when opened, and not scroll the bottom sheet to Expanded State when I scroll the contents up like this result.

Expectation

I expected the above code should behave like this expectation

Does any one have the solution for this situation? Thank you

Hoang Thinh
  • 111
  • 1
  • 12

1 Answers1

1

So after more than a week, I found a solution for this problem, I override the onCreateDialog method like below

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    // This will disable the behavior of bottom sheet to have a flat corner
    //https://github.com/material-components/material-components-android/pull/437#issuecomment-678742683

    val dialog = BottomSheetDialog(requireContext(), theme)
    dialog.behavior.disableShapeAnimations()

    dialog.setOnShowListener { dialogInterface ->

        val bottomSheetDialog = dialogInterface as BottomSheetDialog
        val parentLayout =
            bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
        parentLayout?.let { it1 ->
            val behaviour = BottomSheetBehavior.from(it1)
            it1.layoutParams.also {
                it.height =
                    context?.resources?.displayMetrics?.heightPixels?.times(0.9)?.toInt()!!
            }
            behaviour.peekHeight =
                context?.resources?.displayMetrics?.heightPixels?.times(0.6)?.toInt()!!
        }
    }
    return dialog
}

What is happening in this code?

I was NOT use the dialog provider by BottomSheetDialog neither super.onCreateDialog()

Instead I used BottomSheetDialog(context,theme) in order to create my custom bottom sheet theme.

Then, I set listener for dialog, get parent layout and bottom sheet dialog like below. The most important part is dealing with behavior.

After achieved the behavior correctly, you are done. Setting it's state to be expanded as recommended by @Nitish Chaudhary and setting the layout height to the height that you want. That's all I need, also this code below is reusable to me wherever you follow the modal bottom sheet XML layout rule.

Happy coding.

Hoang Thinh
  • 111
  • 1
  • 12