Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ public abstract static class BottomSheetCallback {

private static final int NO_WIDTH = -1;

private static final int NO_HEIGHT = -1;

private boolean fitToContents = true;

private boolean updateImportantForAccessibilityOnSiblings = false;
Expand All @@ -222,6 +224,8 @@ public abstract static class BottomSheetCallback {

private int maxWidth = NO_WIDTH;

private int maxHeight = NO_HEIGHT;

private int gestureInsetBottom;
private boolean gestureInsetBottomIgnored;
private boolean paddingBottomSystemWindowInsets;
Expand Down Expand Up @@ -324,6 +328,12 @@ public BottomSheetBehavior(@NonNull Context context, @Nullable AttributeSet attr
R.styleable.BottomSheetBehavior_Layout_android_maxWidth, NO_WIDTH));
}

if (a.hasValue(R.styleable.BottomSheetBehavior_Layout_android_maxHeight)) {
setMaxHeight(
a.getDimensionPixelSize(
R.styleable.BottomSheetBehavior_Layout_android_maxHeight, NO_HEIGHT));
}

TypedValue value = a.peekValue(R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight);
if (value != null && value.data == PEEK_HEIGHT_AUTO) {
setPeekHeight(value.data);
Expand Down Expand Up @@ -453,6 +463,18 @@ public void run() {
}
});
}

int height = child.getMeasuredHeight();
if (height > maxHeight && maxHeight != NO_HEIGHT) {
final ViewGroup.LayoutParams lp = child.getLayoutParams();
lp.height = maxHeight;
child.post(new Runnable() {
@Override
public void run() {
child.setLayoutParams(lp);
}
});
}
}
if (viewDragHelper == null) {
viewDragHelper = ViewDragHelper.create(parent, dragCallback);
Expand Down Expand Up @@ -829,6 +851,28 @@ public int getMaxWidth() {
return maxWidth;
}

/**
* Sets the maximum height of the bottom sheet.
* This method should be called before {@link BottomSheetDialog#show()} in order for the height to
* be adjusted as expected.
*
* @param maxHeight The maximum height in pixels to be set
* @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_android_maxHeight
* @see #getMaxHeight()
*/
public void setMaxHeight(@Px int maxHeight) {
this.maxHeight = maxHeight;
}

/**
* Returns the bottom sheet's maximum height, or -1 if no maximum height is set.
*
* @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_android_maxHeight
* @see #setMaxHeight(int)
*/
@Px
public int getMaxHeight() { return maxHeight; }

/**
* Sets the height of the bottom sheet when it is collapsed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<attr name="paddingTopSystemWindowInsets" />
<attr name="android:elevation"/>
<attr name="android:maxWidth"/>
<attr name="android:maxHeight"/>

</declare-styleable>

Expand Down