Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I create a BottomSheetDialogFragment and I want to adjust it's maximum expanded height. How can I do that? I can retrieve the BottomSheetBehaviour but all I can find is a setter for the peek height but nothing for the expanded height.

public class DialogMediaDetails extends BottomSheetDialogFragment {     @Override     public void setupDialog(Dialog dialog, int style)     {         super.setupDialog(dialog, style);         View view = View.inflate(getContext(), R.layout.dialog_media_details, null);         dialog.setContentView(view);          ...          View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);         BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);         behavior.setPeekHeight(...);         // how to set maximum expanded height???? Or a minimum top offset?      } } 

EDIT

Why do I need that? Because I show a BottomSheet Dialog in a full screen activity and it looks bad if the BottomSheet leaves a space on top...

like image 359
prom85 Avatar asked Mar 16 '16 09:03

prom85


People also ask

What is peek height in bottom sheet?

You can just set param "maxHeight" to the root viewgroup of your bottom sheet. android:maxHeight=500dp.

What is behavior_peekHeight?

behavior_peekHeight attribute value used to represent how much pixels the bottom sheet will be visible. I set it to `0dp` because, later in the sample I wanted to demonstrate how to peek the sheet programmatically. BottomSheetBehavior allows the peek height being given programmatically.


2 Answers

The height is being wrapped because the inflated view is added to the FrameLayout which has layout_height=wrap_content. See FrameLayout (R.id.design_bottom_sheet) at https://github.com/dandar3/android-support-design/blob/master/res/layout/design_bottom_sheet_dialog.xml.

The class below makes the bottom sheet full screen, background transparent, and fully expanded to the top.

public class FullScreenBottomSheetDialogFragment extends BottomSheetDialogFragment {       @CallSuper     @Override     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {         super.onViewCreated(view, savedInstanceState);         ButterKnife.bind(this, view);     }       @Override     public void onStart() {         super.onStart();         Dialog dialog = getDialog();          if (dialog != null) {             View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);             bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;         }         View view = getView();         view.post(() -> {             View parent = (View) view.getParent();             CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();             CoordinatorLayout.Behavior behavior = params.getBehavior();             BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior;             bottomSheetBehavior.setPeekHeight(view.getMeasuredHeight());             ((View)bottomSheet.getParent()).setBackgroundColor(Color.TRANSPARENT)          });     }  } 

--- EDIT Aug 30, 2018 --- I realized a year later that the background was colored on the wrong view. This dragged the background along with the content while a user was dragging the dialog. I fixed it so that the parent view of the bottom sheet is colored.

like image 60
Rubin Yoo Avatar answered Oct 04 '22 03:10

Rubin Yoo


I found a much simpler answer; in your example where you obtain the FrameLayout for the bottom sheet using this code

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

you can then set the height on the layout params for that View to whatever height you want to set the expanded height to.

bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT; 
like image 31
Jason Sznol Avatar answered Oct 04 '22 03:10

Jason Sznol