Closed renanferrari closed 3 years ago
I think you can set it in xml using app:behavior_skipCollapsed="true"
Also in situations where such an xml attribute is not available, using BindingAdapter
s can help hide the "hackish" implementation details.
Here you can find the full list of xml attributes you can use to customize bottomsheet behavior: https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/bottomsheet/res/values/attrs.xml
@AradiPatrik Thank you, but I don't believe your answer addresses the main point of my question.
My question was specific to BottomSheetDialogFragment
, so XML attributes are unavailable. I'm also not looking for ways to "hide" the details of my implementation.
I just want to understand, from the perspective of the maintainers of this library, what's the recommended way to achieve what I need in the context I have described.
I facing same problem, are behavior attrs cannot be configured via xml for BottomSheetDialogFragment
?
@SYtor Nope, it uses the default ones and you can only access/change them programmatically, like shown in my original post.
I've found a convenient way to config bottom sheet behavior using styles.
styles.xml
<style name="Widget.MyApp.BottomSheet.Modal" parent="Widget.MaterialComponents.BottomSheet.Modal">
<item name="behavior_skipCollapsed">true</item>
<item name="behavior_fitToContents">true</item>
<item name="behavior_peekHeight">1000dp</item> // yep, that helped to skip collapsed state at initial
<item name="behavior_hideable">true</item>
</style>
Go inside Widget.MaterialComponents.BottomSheet.Modal
to see what settings you can modify.
Theme.Design.BottomSheetDialog
and set that you want to override bottom sheet's style with you own. Also can be placed in styles.xml
<style name="Theme.MyApp.BottomSheetDialog" parent="Theme.Design.BottomSheetDialog">
<item name="bottomSheetStyle">@style/Widget.MyApp.BottomSheet.Modal</item>
</style>
themes.xml
(hope you follow Google's recommendations about packaging styles&themes)
<style name="Base.Theme.MyApp" parent="Base.Theme.Root">
... too many other things
<item name="bottomSheetDialogTheme">@style/Theme.MyApp.BottomSheetDialog</item>
</style>
@SYtor @renanferrari checkout my previous comment
P.S. I'm using the latest stable version of MaterialDesign dependency
com.google.android.material:material:1.1.0
@renanferrari inside onViewCreated
i used that
val behavior: BottomSheetBehavior<*> = (dialog as BottomSheetDialog).behavior
behavior.halfExpandedRatio = 0.3f
behavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
i don't know if it is the correct way
@renanferrari seems It's possible to customize behavior as in the following code:
val callback = object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
onStateChanged(binding, newState)
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {
onSlide(binding, slideOffset)
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val behavior: BottomSheetBehavior<*> = (dialog as BottomSheetDialog).behavior
behavior.addBottomSheetCallback(behaviorCallback)
...
}
Thanks to @xanscale for the idea.
@renanferrari Google use this xml to create a dialog inside BottomSheetDialogFragment https://github.com/dandar3/android-support-design/blob/master/res/layout/design_bottom_sheet_dialog.xml
you can try to overwrite @string/bottom_sheet_behavior, just add this to your resources:
<string name="bottom_sheet_behavior" translatable="false">your.custom.Behavior</string>
I think the way @xanscale posted is the recommended way from a library maintainer's perspective.
The solution @DmitryTankovich posted should also work but it's less recommended since it's fragile and can be easily broken by an internal change.
I'll close the issue. Let us know if you have more questions. : )
The following code called from onCreateView
((BottomSheetDialog) getDialog()).getBehavior().setState(BottomSheetBehavior.STATE_EXPANDED);
Seems to work fine for me.
Let's say I want my bottom sheet to skip its collapsed state. If I used a
BottomSheetBehavior
I could just setskipCollapse
totrue
. What's the recommended way to achieve that on aBottomSheetDialogFragment
?So far, I've been retrieving and modifying the
BottomSheetDialogFragment
's innerBottomSheetBehavior
myself, like so:However, this approach seems kind of hackish to me. Is there any alternative or is this the recommended way to achieve what I want?