Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change height of BottomSheetBehavior

I'm using the BottomSheetBehavior from Google recently released AppCompat v23.2. The height of my bottom sheet depends on the content displayed inside of the bottom sheet (similar to the what Google does themselves in their Maps app).

It works fine with the data loaded initially, but my application changes the content displayed during runtime and when this happens the bottom sheet retains at it's old height, which either leads to unused space at the bottom or a cut of view.

Is there any way to inform the bottom sheet layout to recalculate the height used for expanded state (when height of the ViewGroup is set to MATCH_HEIGHT) or any way to manually set the required height?


EDIT: I also tried to manually call invalidate() on the ViewGroup and the parent of it but without any success.

like image 648
miho Avatar asked Feb 28 '16 17:02

miho


2 Answers

I had the same problem with RelativeLayout as my bottom sheet. The height won't be recalculated. I had to resort to setting the height by the new recalculated value and call BottomSheetBehavior.onLayoutChild.

This is my temporary solution:

coordinatorLayout = (CoordinatorLayout)findViewById(R.id.coordinator_layout); bottomSheet = findViewById(R.id.bottom_sheet);  int accountHeight = accountTextView.getHeight(); accountTextView.setVisibility(View.GONE);  bottomSheet.getLayoutParams().height = bottomSheet.getHeight() - accountHeight; bottomSheet.requestLayout(); behavior.onLayoutChild(coordinatorLayout, bottomSheet, ViewCompat.LAYOUT_DIRECTION_LTR); 
like image 101
Phyrum Tea Avatar answered Sep 27 '22 19:09

Phyrum Tea


You can use BottomSheetBehavior#setPeekHeight for that.

FrameLayout bottomSheet = (FrameLayout) findViewById(R.id.bottom_sheet); BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet); behavior.setPeekHeight(newHeight); 

This does not automatically move the bottom sheet to the peek height. You can call BottomSheetBehavior#setState to adjust your bottom sheet to the new peek height.

behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 
like image 40
Yuichi Araki Avatar answered Sep 27 '22 19:09

Yuichi Araki