Bottom Sheet Dialog implementation in kotlin

· 1 min read
Bottom Sheet Dialog implementation in kotlin
Display bottom sheet dialog in Kotlin

To create a bottom sheet dialog in Kotlin, you can use the BottomSheetDialog class from the Android Support Library, which is a built-in package.

Here's an example of how you can use a BottomSheetDialog in Kotlin:

val bottomSheetDialog = BottomSheetDialog(this)
val view = layoutInflater.inflate(R.layout.example_bottom_sheet, null)
bottomSheetDialog.setContentView(view)
bottomSheetDialog.show()

Some key points in the above examples:

  1. We create a new instance of BottomSheetDialog and pass it the current context.
  2. Then inflate the layout for the bottom sheet and set it as the content view of the dialog using the setContentView() method.
  3. Finally show the bottom sheet dialog using the show() method.

Moreover, we can customize the behavior of the bottom sheet dialog by setting the BottomSheetBehavior of the bottom sheet's view. For example, you can set the state of the behavior to STATE_COLLAPSED to make the bottom sheet collapsed by default, or set the peekHeight to specify the height of the collapsed bottom sheet.

val bottomSheetBehavior = BottomSheetBehavior.from(view.findViewById(R.id.bottom_sheet))
bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
bottomSheetBehavior.peekHeight = 200