2

I have a bottomsheet behavior in a linear layout. I need to set the peak height of the bottom sheet to below this button, so it works great on all screens instead of giving a constant value.

The bottom sheet is inside a CoordinatorLayout that has that weird transparent big space. I want the sheet to fill up that space
The rounded bottom sheet's peak height should match up to below the Fireworks text

I have tried with setPeekHeight(ContainerOfBottomSheet.getMeasuredHeight()); but it seems to return 0 or an extremely big value. How can I do that?

UPDATE
This is my code:

    private void SettingSheet(LinearLayout sheet, final CoordinatorLayout c) {
      final BottomSheetBehavior beh = BottomSheetBehavior.from(sheet);
        
      c.measure(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
      CHeight = c.getMeasuredHeight();
        
      beh.setPeekHeight(CHeight);
            }

//This is making the big value I have talked about.

Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
Rocks123
  • 21
  • 4

1 Answers1

0

You can set the peek height of a BottomSheet to a specific view using the setPeekHeight method of the BottomSheetBehavior class. Here's an example:

ViewTreeObserver vto = target_view.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            target_view.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            // Get the height of the target view from the top of its parent view
            int targetViewHeight = target_view.getTop();
            // Set the peek height of the BottomSheet to the height of the target view
            bottomSheetBehavior.setPeekHeight(targetViewHeight);
            // Set the state of the BottomSheet to collapsed
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

        }
    });
Saeid Mohammadi
  • 319
  • 1
  • 2
  • 11