Notification texts go here Contact Us Buy Now!

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

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

Problem: You want to adjust the maximum expanded height of the BottomSheetDialogFragment. The default behavior is for the sheet to expand to the height of its content, but you want to be able to specify a custom height.

Solution: BottomSheetDialogFragment uses a BottomSheetBehavior to control its behavior. You can access this behavior by calling BottomSheetBehavior.from(View) on the BottomSheet view. Once you have the behavior, you can set the min top offset, which is the maximum expanded height of the sheet, using the setMinOffset() method.

  1. Create a Java class that extends CoordinatorLayout.Behavior<V>.
  2. Copy and paste the code from the default BottomSheetBehavior file to your new one.
  3. Modify the method clampViewPositionVertical with the following code:
```html @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:
```html public static final int STATE_ANCHOR_POINT = X; ```
  1. Modify the methods onLayoutChild, onStopNestedScroll, BottomSheetBehavior<V> from(V view) and setState (optional).

Here is an example of how to use the custom behavior:

```html BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet); behavior.setMinOffset(100); ```

This will set the minimum expanded height of the sheet to 100 pixels.

Another Solution:

You can also set the expanded height of the sheet by modifying the layout params of the BottomSheet view. To do this, you need to get the FrameLayout that contains the sheet using the following code:

```html View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet); ```

Once you have the FrameLayout, you can then set the height of the sheet using the following code:

```html bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT; ```

This will cause the sheet to expand to the full height of the screen.

Note: Keep in mind that the expanded height of the sheet is also affected by the peekHeight property of the BottomSheetBehavior. If you set the peekHeight to a value that is greater than the min top offset, the sheet will expand to the peekHeight instead of the min top offset.

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.