11

I am able to open my BottomSheetDialogFragment with

val bottomSheet = BottomSheetFragment()
bottomSheet.show(fragmentManager!!, "BottomSheet")

but it only opens to show half of its content - I would like it to expand on opening to the full height of the screen without having to drag it up.

I have looked around and it seems one way is to set the BottomSheetBehavior state to STATE_EXPANDED, but I have not been able to find a solution on how to do this in Kotlin.

Any help would be appreciated!

SQLol
  • 227
  • 2
  • 4
  • 12

1 Answers1

40

You can set the BottomSheetBehavior state by placing this inside onViewCreated of your BottomSheetDialogFragment.

dialog.setOnShowListener { dialog ->
    val d = dialog as BottomSheetDialog
    val bottomSheet = d.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout
    val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
    bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
}

You may also want to set the peek height to the height of your dialog to prevent the dialog getting stuck half way when attempting to dismiss it.

bottomSheetBehavior.peekHeight = bottomSheet.height
Damon Baker
  • 887
  • 7
  • 9
  • 4
    Perfect, thank you! Just had to change the findViewbyId argument to com.google.android.material.R.id.design_bottom_sheet as I'm using Androidx – SQLol Feb 12 '19 at 11:03
  • Works for me, just had to give the dialog `.xml` layout file some minimum height, as I don't have much content. – Ofcourse Feb 07 '20 at 13:53
  • 1
    Worked like a charm, likewise @SQLol had to change it from "R.id.design_bottom_sheet" to "com.google.android.material.R.id.design_bottom_sheet" thanks – Simon Gomes Mar 04 '20 at 09:38
  • not able to show bottom sheet. bottomSheetDialogFragment.show(fragmentManager!!, "BottomSheet") showing error in this line – Josss Mar 23 '21 at 08:48
  • The issue with setting `bottomSheetBehavior.peekHeight = bottomSheet.height` is that when you swipe down to dismiss, the dialog does not actually dismiss and you are left with a greyed overlap on the screen. – Jean Tadebois Jun 14 '21 at 10:44
  • 1
    In order to avoid having a greyed overlap, instead use `bottomSheetBehavior.skipCollapsed = true`. – Jean Tadebois Jun 14 '21 at 13:06
  • Please update the answer. @JeanTadebois's suggestion of setting BottomSheetBehavior.skipCollapsed to true to prevent the dialog from being stuck half way is the better/cleaner choice. – dknchris Dec 20 '21 at 21:04