Skip to content

Instantly share code, notes, and snippets.

@ForceTower
Created July 30, 2021 13:35
Show Gist options
  • Save ForceTower/47d589e6d2e8c5b05f908fd2213c4310 to your computer and use it in GitHub Desktop.
Save ForceTower/47d589e6d2e8c5b05f908fd2213c4310 to your computer and use it in GitHub Desktop.
Always rounded corners
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
Timber.d("State changed $newState")
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
Timber.d("State is expanded :D")
val shape = createShape(bottomSheet)
ViewCompat.setBackground(bottomSheet, shape)
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) = Unit
})
return dialog
}
private fun createShape(view: View): MaterialShapeDrawable {
val model = ShapeAppearanceModel.builder(requireContext(), 0, R.style.Widget_App_ShapeAppearanceBottomSheetDialog)
.build()
val current = view.background as MaterialShapeDrawable
val next = MaterialShapeDrawable(model)
next.initializeElevationOverlay(context)
next.fillColor = current.fillColor
next.tintList = current.tintList
next.elevation = current.elevation
next.strokeWidth = current.strokeWidth
next.strokeColor = current.strokeColor
return next
}
<style name="ThemeOverlay.App.BottomSheetDialog" parent="@style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="bottomSheetStyle">@style/Widget.App.BottomSheet</item>
</style>
<style name="Widget.App.BottomSheet" parent="Widget.MaterialComponents.BottomSheet">
<item name="shapeAppearanceOverlay">@style/Widget.App.ShapeAppearanceBottomSheetDialog</item>
</style>
<style name="Widget.App.ShapeAppearanceBottomSheetDialog" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">16dp</item>
<item name="cornerSizeTopLeft">16dp</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment