Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open BottomSheetDialogFragment fully expanded in Kotlin?

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!

like image 791
SQLol Avatar asked Feb 12 '19 02:02

SQLol


1 Answers

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
like image 112
Damon Baker Avatar answered Nov 17 '22 15:11

Damon Baker