Skip to content

Commit 5aebe1a

Browse files
melaniegoetzcketcham
authored andcommitted
API to set height of a half expanded sheet, in proportion to parent height
PiperOrigin-RevId: 239689682
1 parent abbec48 commit 5aebe1a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ public abstract static class BottomSheetCallback {
195195

196196
int halfExpandedOffset;
197197

198+
float halfExpandedRatio = 0.5f;
199+
198200
int collapsedOffset;
199201

200202
boolean hideable;
@@ -261,6 +263,7 @@ public BottomSheetBehavior(Context context, AttributeSet attrs) {
261263
setSkipCollapsed(
262264
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_skipCollapsed, false));
263265
setSaveFlags(a.getInt(R.styleable.BottomSheetBehavior_Layout_behavior_saveFlags, SAVE_NONE));
266+
setHalfExpandedRatio(a.getFloat(R.styleable.BottomSheetBehavior_Layout_behavior_halfExpandedRatio, 0.5f));
264267
a.recycle();
265268
ViewConfiguration configuration = ViewConfiguration.get(context);
266269
maximumVelocity = configuration.getScaledMaximumFlingVelocity();
@@ -335,7 +338,7 @@ public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirect
335338
parentWidth = parent.getWidth();
336339
parentHeight = parent.getHeight();
337340
fitToContentsOffset = Math.max(0, parentHeight - child.getHeight());
338-
halfExpandedOffset = parentHeight / 2;
341+
calculateHalfExpandedOffset();
339342
calculateCollapsedOffset();
340343

341344
if (state == STATE_EXPANDED) {
@@ -692,6 +695,32 @@ public final int getPeekHeight() {
692695
return peekHeightAuto ? PEEK_HEIGHT_AUTO : peekHeight;
693696
}
694697

698+
/**
699+
* Determines the height of the BottomSheet in the {@link #STATE_HALF_EXPANDED} state. The
700+
* material guidelines recommended a value of 0.5, which results in the sheet filling half of the
701+
* parent. The height of the BottomSheet will be smaller as this ratio is decreased and taller as
702+
* it is increased. The default value is 0.5.
703+
*
704+
* @param ratio a float between 0 and 1, representing the {@link #STATE_HALF_EXPANDED} ratio.
705+
* @attr com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_halfExpandedRatio
706+
*/
707+
public void setHalfExpandedRatio(float ratio) {
708+
709+
if ((ratio <= 0) || (ratio >= 1)) {
710+
throw new IllegalArgumentException("ratio must be a float value between 0 and 1");
711+
}
712+
this.halfExpandedRatio = ratio;
713+
}
714+
715+
/**
716+
* Gets the ratio for the height of the BottomSheet in the {@link #STATE_HALF_EXPANDED} state.
717+
*
718+
* @attr com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_halfExpandedRatio
719+
*/
720+
public float getHalfExpandedRatio() {
721+
return halfExpandedRatio;
722+
}
723+
695724
/**
696725
* Sets whether this bottom sheet can hide when it is swiped down.
697726
*
@@ -895,6 +924,10 @@ private void calculateCollapsedOffset() {
895924
}
896925
}
897926

927+
private void calculateHalfExpandedOffset() {
928+
this.halfExpandedOffset = (int) (parentHeight * halfExpandedRatio);
929+
}
930+
898931
private void reset() {
899932
activePointerId = ViewDragHelper.INVALID_POINTER;
900933
if (velocityTracker != null) {

lib/java/com/google/android/material/bottomsheet/res/values/attrs.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
<attr name="behavior_skipCollapsed" format="boolean"/>
3434
<!-- Whether height of expanded sheet wraps content or not -->
3535
<attr name="behavior_fitToContents" format="boolean"/>
36+
<!-- The ratio to be used to set the height of half-expanded state in proportion to parent, when
37+
fitToContents is false. Defaults to true half, 0.5, if not explicitly set. Ratio must be a
38+
float value between 0 and 1 and produce a half-expanded state height larger than the
39+
peek height for the half-expanded state to be operational -->
40+
<attr name="behavior_halfExpandedRatio" format="reference|float"/>
3641
<!-- Shape appearance style reference for BottomSheet. Attribute declaration is in the shape
3742
package. -->
3843
<attr name="shapeAppearance"/>

0 commit comments

Comments
 (0)