Reputation: 213
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 ;)
Upvotes: 21
Views: 8930
Reputation: 1
To achieve mentioned functionality you can make use of constrainedHeight set to true on the recycler view. Also, constrain recyclers bottom to top of button and button constraint to the end. Refer to the code below
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view"
app:layout_constraintTop_toTopOf="parent"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="@id/button"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 0
Reputation: 268
I am finding a solution for sticky bottom button, although want to answer for your additional question about setting up the Bottom sheet margin.
So you cannot add margins to your bottom sheet, it is basically being handled by state of Bottom sheet.
You can explicitly set the state of Bottom sheet by following:
val bottomSheet = dialog!!.findViewById<View>(design_bottom_sheet) as FrameLayout
val behavior: BottomSheetBehavior<*> = BottomSheetBehavior.from<View>(bottomSheet)
behavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
Upvotes: 1
Reputation: 105
Might take a while, but I faced same problem so I decided to write an article: https://dorianpavetic.medium.com/android-sticky-view-at-the-bottom-of-bottom-sheet-dialog-fragment-ac91242bc1b7
Upvotes: 1
Reputation: 2551
Thanks, @Gnocalo, post. I have implemented it and please find it in my github
Steps:
parent
view BottomSheetDialogFragment
parent
viewUpvotes: 8
Reputation: 312
the 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
Upvotes: 5