Skip to content

Instantly share code, notes, and snippets.

@n0m0r3pa1n
Last active October 2, 2023 01:53
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save n0m0r3pa1n/c1a28dd7c6098bf5f73ad27d61bf242e to your computer and use it in GitHub Desktop.
Save n0m0r3pa1n/c1a28dd7c6098bf5f73ad27d61bf242e to your computer and use it in GitHub Desktop.
Bottom sheet dialog with expanding and fullscreen display
fun Fragment.showBottomSheetDialog(
@LayoutRes layout: Int,
@IdRes textViewToSet: Int? = null,
textToSet: String? = null,
fullScreen: Boolean = true,
expand: Boolean = true
) {
val dialog = BottomSheetDialog(context!!)
dialog.setOnShowListener {
val bottomSheet: FrameLayout = dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet) ?: return@setOnShowListener
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
if (fullScreen && bottomSheet.layoutParams != null) { showFullScreenBottomSheet(bottomSheet) }
if (!expand) return@setOnShowListener
bottomSheet.setBackgroundResource(android.R.color.transparent)
expandBottomSheet(bottomSheetBehavior)
}
@SuppressLint("InflateParams") // dialog does not need a root view here
val sheetView = layoutInflater.inflate(layout, null)
textViewToSet?.also {
sheetView.findViewById<TextView>(it).text = textToSet
}
sheetView.findViewById<ImageView>(R.id.closeButton)?.setOnClickListener {
dialog.dismiss()
}
dialog.setContentView(sheetView)
dialog.show()
}
private fun showFullScreenBottomSheet(bottomSheet: FrameLayout) {
val layoutParams = bottomSheet.layoutParams
layoutParams.height = Resources.getSystem().displayMetrics.heightPixels
bottomSheet.layoutParams = layoutParams
}
private fun expandBottomSheet(bottomSheetBehavior: BottomSheetBehavior<FrameLayout>) {
bottomSheetBehavior.skipCollapsed = true
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment