Notification texts go here Contact Us Buy Now!

BottomSheetDialogFragment - How to set expanded height (or min top offset)

Sure, here's the reformatted answer in HTML, using a block to represent codes:

Question: I want to adjust the BottomSheet's maximum expanded height. How can I do that?

Answer: Both BottomSheet and BottomSheetDialogFragment use a BottomSheetBehavior that you can find in Support Library 23.x. That Java class has two different uses for mMinOffset. One defines the area of the parent it will use to draw its content (maybe a NestedScrollView). The other defines the expanded anchor point, meaning if you slide it up to form STATE_COLLAPSED, it will animate your BottomSheet until it reaches this anchor point BUT you can still keep sliding up to cover all parent height (CoordiantorLayout Height).

To modify the expanded anchor point:

  1. Create a Java class and extend it from CoordinatorLayout.Behavior<V>
  2. Copy-paste code from the default BottomSheetBehavior file to your new one.
  3. Modify the method clampViewPositionVertical with the following code:
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
    return constrain(top, mMinOffset, mHideable ? mParentHeight : mMaxOffset);
}
int constrain(int amount, int low, int high) {
    return amount < low ? low : (amount > high ? high : amount);
}
  1. Add a new state:
    public static final int STATE_ANCHOR_POINT = X;
    
  2. Modify the next methods: onLayoutChild, onStopNestedScroll, BottomSheetBehavior<V> from(V view), and setState (optional)

Here is a simpler answer that involves setting the height on the layout params of the BottomSheet's FrameLayout:

View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;

Another option is to use the BottomSheetBehavior's peekHeight property:

BottomSheetBehavior.from(bottomSheet).peekHeight = 250;

You can also set the peek height in Kotlin using the BottomSheetDialogFragment's onViewCreated() method:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {

            view.viewTreeObserver.removeOnGlobalLayoutListener(this)

            val dialog = dialog as BottomSheetDialog
            val bottomSheet = dialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
            val behavior = BottomSheetBehavior.from(bottomSheet!!)
            behavior.state = BottomSheetBehavior.STATE_EXPANDED

            val newHeight = activity?.window?.decorView?.measuredHeight
            val viewGroupLayoutParams = bottomSheet.layoutParams
            viewGroupLayoutParams.height = newHeight ?: 0
            bottomSheet.layoutParams = viewGroupLayoutParams
        }
    })
    dialogView = view
}

And don't forget to remove viewTreeObserver in onDestroyView().

Another approach is to get a reference to the sheet behavior and set the expandedOffset to the desired pixels:

private val behavior by lazy { (dialog as BottomSheetDialog).behavior }
behavior.isFitToContents = false
behavior.expandedOffset = 100

And for Jetpack Compose views inside BottomSheetDialogFragment, you can override onCreateDialog to set the peekHeight:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = super.onCreateDialog(savedInstanceState)
    dialog.setOnShowListener { dialogInterface ->
        val bottomSheetDialog = dialogInterface as? BottomSheetDialog
        bottomSheetDialog?.behavior?.setPeekHeight(3000)
    }
    return dialog
}

I hope this helps! Let me know if you have any other questions.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.