3

How do I make a bottomSheet take up the full height of the screen? Setting the peek height has no effect.

Any help would be appreciated.

bottomSheetDialogFragment.getDialog().setOnShowListener((dialog) ->
{
    final BottomSheetDialog bottomSheetDialog = (BottomSheetDialog)dialog;
    final FrameLayout bottomSheet = bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
    if (bottomSheet != null)
    {
        final BottomSheetBehavior<View> behavior = BottomSheetBehavior.from(bottomSheet);
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        behavior.setPeekHeight(30000); // no effect, bottom sheet does not span entire height of screen
    }
});

BottomSheet Layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <!-- rest of layout not shown -->
    <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bottomSheetHandle"
        tools:layout_height="48dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
VIN
  • 6,385
  • 7
  • 38
  • 77
  • why not you use simple dialoge with full screen with bottom to top animation – Amit pandey Feb 26 '20 at 05:15
  • Does this answer your question? [How can I make BottomSheetDialog match parent height (full screen)](https://stackoverflow.com/questions/57769252/how-can-i-make-bottomsheetdialog-match-parent-height-full-screen) – zihadrizkyef Mar 08 '22 at 15:30

4 Answers4

4

You could get the metrics to have access to the height of the screen in pixels and use that reference to set the height of your bottomsheet.

Get Metrics

val metrics = DisplayMetrics()
requireActivity().windowManager?.defaultDisplay?.getMetrics(metrics)

Set state and peekHeight of dialog

bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheetDialog.behavior.peekHeight = metrics.heightPixels

Set height of your view, notice how we are setting this height as the same of the peekHeight of the dialog. I found this the best way when you want a single size for your BottomSheetDialog

bottomSheet.layoutParams.height = metrics.heightPixels
bottomSheet.requestLayout()
Gio
  • 190
  • 2
  • 11
2

at first inside onCreateDialog

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    ...
    bottomSheetBehavior?.skipCollapsed = true
    bottomSheetBehavior?.peekHeight = Resources.getSystem().displayMetrics.heightPixels
    bottomSheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
    return bottomSheet
}

afterward, use this on start method

/**
 * to make sheet height full screen
 * */
override fun onStart() {
    super.onStart()
    val metrics = DisplayMetrics()
    requireActivity().windowManager?.defaultDisplay?.getMetrics(metrics)
    binding.rootContainer.layoutParams.height = metrics.heightPixels
    binding.rootContainer.requestLayout()
}

I hope it works fine because it work correctly with me ;)

Mostafa Anter
  • 3,445
  • 24
  • 26
2

to accomplish that you can set match_parent to layout params of bottom sheet like this:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
                    val dialog = BottomSheetDialog(requireContext(), theme)
                    dialog.setOnShowListener {
                
                        val bottomSheetDialog = it as BottomSheetDialog
                        val parentLayout =
                            bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
                        parentLayout?.let { it ->
                            val behaviour = BottomSheetBehavior.from(it)
                            setupFullHeight(it)
                            behaviour.state = BottomSheetBehavior.STATE_EXPANDED
                        }
                    }
                    return dialog
                }
                
                private fun setupFullHeight(bottomSheet: View) {
                    val layoutParams = bottomSheet.layoutParams
                    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
                    bottomSheet.layoutParams = layoutParams
                }
    }
YotiS
  • 71
  • 7
0
// add this code into your class

    @Override
        public void onStart() {
            super.onStart();
            Dialog dialog = getDialog();
            View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
            if (dialog != null) {
        
                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)
    
            });
        }
Gayathri
  • 249
  • 3
  • 13