material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[BottomSheetBehavior] Allow peek height to be determined by child view

Open OxygenCobalt opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe. In my app, the bottom sheet has a "bar" view that takes up all of the peek height. However, it seems that the only way to modify the peek height to accommodate the bar is by setting it to a fixed value. This is not ideal, as my bar contains text, and if the text scale is increased enough, it will result in the hard-coded size in DP not lining up with the actual size in DP and SP. This would cause a visual bug as the bar view extends into the navigation bar.

Describe the solution you'd like I want another option for peekHeight (firstChild perhaps?) that takes the first child in a ViewGroup and uses that to calculate the peek height instead. That way, the height can be automatically determined from the "bar" view, which is the first view in my Bottom Sheet.

Describe alternatives you've considered I can override the peek height by subclassing the behavior and overriding onLayoutChild as such. I would prefer this to be native to the behavior, however.

override fun onLayoutChild(parent: CoordinatorLayout, child: V, layoutDirection: Int): Boolean {
    val success = super.onLayoutChild(parent, child, layoutDirection)
    (child as? ViewGroup)?.getChildAt(0)?.let {
        peekHeight = it.height
    }
    return success
}

OxygenCobalt avatar Jun 27 '22 17:06 OxygenCobalt

Hi, I think the request kind of makes sense. But I can't figure out a good API on top of my head.

Can you describe in more details about the API in your mind?

drchen avatar Jun 29 '22 11:06 drchen

@drchen I'd imagine implementing this would require behavior_peekHeight to be deprecated in favor of new attributes. I propose something like this:

<!-- The strategy to use when peeking a view. -->
<attr name="behavior_peekMode">
    <!-- Peek at a fixed height specified by behavior_peekFixedHeight -->
    <enum name="fixed" value="0" />
    <!-- Peek at the 16:9 keyline. -->
    <enum name="auto" value="1" />
    <!-- Peek at the height of an arbitrary child in the bottom sheet. Requires the target to be a ViewGroup. -->
    <enum name="child" value="2" />
</attr>

<!-- The height to use with the "fixed" peek mode. This has no effect with other peek modes. -->
<attr name="behavior_peekFixedHeight" format="dimension" />

<!-- A specific view ID corresponding with the child view that should be used with the "child" peek mode. Defaults to the first child if not set. This has no effect with other peek modes. -->
<attr name="behavior_peekChildId" format="reference" />

For compat, the old attribute:

app:behavior_peekHeight="72dp"

Would now map to:

app:behavior_peekMode="fixed"
app:behavior_peekFixedHeight="72dp"

OxygenCobalt avatar Jun 29 '22 14:06 OxygenCobalt

Cool. Thanks. It's clear. I'll raise this to the team and see what they think.

drchen avatar Jun 29 '22 18:06 drchen