Last active
August 29, 2024 09:33
-
-
Save n0m0r3pa1n/c1a28dd7c6098bf5f73ad27d61bf242e to your computer and use it in GitHub Desktop.
Bottom sheet dialog with expanding and fullscreen display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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