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:
- Create a Java class and extend it from CoordinatorLayout.Behavior<V>
- Copy-paste code from the default BottomSheetBehavior file to your new one.
- 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);
}
- Add a new state:
public static final int STATE_ANCHOR_POINT = X;
- 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.