I have some trouble displaying a button in my BottomSheetDialogFragment
. I want it to stick to the bottom of my bottom sheet, no matter if the sheet is expanded or collapsed.
See the picture below :
(I used sketch to create this)
Any tips or tricks ?
And just in case, if you know how to add top margin to the bottomsheetdialog
, I'd love to know this too ;)
Thanks, @Gnocalo, post. I have implemented it and please find it in my github
Steps:
parent
view BottomSheetDialogFragment
parent
viewthe way I tackled this issue was the following:
Example code following:
val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
bottomSheetDialog.setOnShowListener {
val dialog = it as BottomSheetDialog
dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout
val containerLayout: FrameLayout =
dialog.findViewById<FrameLayout>(com.google.android.material.R.id.container) as FrameLayout
val button = binding.submitButton
val parent = button.parent as ViewGroup
parent.removeView(button)
containerLayout.addView(button, containerLayout.childCount)
}
return bottomSheetDialog
This way your bottom sheet will respond to touches normally and the button will stay on it's position on the parent.
If you have any doubt feel free to ask.
EDIT
Don't forget to define layout params/positioning the button for it to be on the bottom of R.id.container
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With