5151import androidx .annotation .IntDef ;
5252import androidx .annotation .NonNull ;
5353import androidx .annotation .Nullable ;
54+ import androidx .annotation .Px ;
5455import androidx .annotation .RestrictTo ;
5556import androidx .annotation .StringRes ;
5657import androidx .annotation .VisibleForTesting ;
@@ -194,6 +195,8 @@ public abstract static class BottomSheetCallback {
194195
195196 private static final int CORNER_ANIMATION_DURATION = 500 ;
196197
198+ private static final int NO_WIDTH = -1 ;
199+
197200 private boolean fitToContents = true ;
198201
199202 private boolean updateImportantForAccessibilityOnSiblings = false ;
@@ -217,6 +220,8 @@ public abstract static class BottomSheetCallback {
217220
218221 private MaterialShapeDrawable materialShapeDrawable ;
219222
223+ private int maxWidth = NO_WIDTH ;
224+
220225 private int gestureInsetBottom ;
221226 private boolean gestureInsetBottomIgnored ;
222227 private boolean paddingBottomSystemWindowInsets ;
@@ -313,6 +318,12 @@ public BottomSheetBehavior(@NonNull Context context, @Nullable AttributeSet attr
313318 this .elevation = a .getDimension (R .styleable .BottomSheetBehavior_Layout_android_elevation , -1 );
314319 }
315320
321+ if (a .hasValue (R .styleable .BottomSheetBehavior_Layout_android_maxWidth )) {
322+ setMaxWidth (
323+ a .getDimensionPixelSize (
324+ R .styleable .BottomSheetBehavior_Layout_android_maxWidth , NO_WIDTH ));
325+ }
326+
316327 TypedValue value = a .peekValue (R .styleable .BottomSheetBehavior_Layout_behavior_peekHeight );
317328 if (value != null && value .data == PEEK_HEIGHT_AUTO ) {
318329 setPeekHeight (value .data );
@@ -399,7 +410,7 @@ public void onDetachedFromLayoutParams() {
399410
400411 @ Override
401412 public boolean onLayoutChild (
402- @ NonNull CoordinatorLayout parent , @ NonNull V child , int layoutDirection ) {
413+ @ NonNull CoordinatorLayout parent , @ NonNull final V child , int layoutDirection ) {
403414 if (ViewCompat .getFitsSystemWindows (parent ) && !ViewCompat .getFitsSystemWindows (child )) {
404415 child .setFitsSystemWindows (true );
405416 }
@@ -429,6 +440,19 @@ public boolean onLayoutChild(
429440 == ViewCompat .IMPORTANT_FOR_ACCESSIBILITY_AUTO ) {
430441 ViewCompat .setImportantForAccessibility (child , ViewCompat .IMPORTANT_FOR_ACCESSIBILITY_YES );
431442 }
443+
444+ // Adjust the width to be at most the maxWidth if needed.
445+ int width = child .getMeasuredWidth ();
446+ if (width > maxWidth && maxWidth != NO_WIDTH ) {
447+ final ViewGroup .LayoutParams lp = child .getLayoutParams ();
448+ lp .width = maxWidth ;
449+ child .post (new Runnable () {
450+ @ Override
451+ public void run () {
452+ child .setLayoutParams (lp );
453+ }
454+ });
455+ }
432456 }
433457 if (viewDragHelper == null ) {
434458 viewDragHelper = ViewDragHelper .create (parent , dragCallback );
@@ -781,6 +805,30 @@ public void setFitToContents(boolean fitToContents) {
781805 updateAccessibilityActions ();
782806 }
783807
808+ /**
809+ * Sets the maximum width of the bottom sheet. The layout will be at most this dimension wide.
810+ * This method should be called before {@link BottomSheetDialog#show()} in order for the width to
811+ * be adjusted as expected.
812+ *
813+ * @param maxWidth The maximum width in pixels to be set
814+ * @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_android_maxWidth
815+ * @see #getMaxWidth()
816+ */
817+ public void setMaxWidth (@ Px int maxWidth ) {
818+ this .maxWidth = maxWidth ;
819+ }
820+
821+ /**
822+ * Returns the bottom sheet's maximum width, or -1 if no maximum width is set.
823+ *
824+ * @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_android_maxWidth
825+ * @see #setMaxWidth(int)
826+ */
827+ @ Px
828+ public int getMaxWidth () {
829+ return maxWidth ;
830+ }
831+
784832 /**
785833 * Sets the height of the bottom sheet when it is collapsed.
786834 *
0 commit comments