Skip to content

Commit 63d01aa

Browse files
leticiarossiymarian
authored andcommitted
[Bottomsheets][Large Screens] Adding a max width for bottom sheets to optimize for large screens.
PiperOrigin-RevId: 368533002 (cherry picked from commit ce928fc)
1 parent c9f7a61 commit 63d01aa

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import androidx.annotation.IntDef;
5252
import androidx.annotation.NonNull;
5353
import androidx.annotation.Nullable;
54+
import androidx.annotation.Px;
5455
import androidx.annotation.RestrictTo;
5556
import androidx.annotation.StringRes;
5657
import 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
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<attr name="paddingRightSystemWindowInsets" />
8383
<attr name="paddingTopSystemWindowInsets" />
8484
<attr name="android:elevation"/>
85+
<attr name="android:maxWidth"/>
8586

8687
</declare-styleable>
8788

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@
2020
<dimen name="design_bottom_sheet_elevation">8dp</dimen>
2121
<dimen name="design_bottom_sheet_peek_height_min">64dp</dimen>
2222

23+
<dimen name="material_bottom_sheet_max_width">640dp</dimen>
24+
2325
</resources>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@
4343
<style name="Widget.MaterialComponents.BottomSheet" parent="Widget.Design.BottomSheet.Modal">
4444
<item name="enforceMaterialTheme">true</item>
4545
<item name="android:background">@null</item>
46+
<item name="android:layout_gravity">center_horizontal</item>
4647
<item name="shapeAppearance">?attr/shapeAppearanceLargeComponent</item>
4748
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MaterialComponents.BottomSheet</item>
4849
<item name="backgroundTint">?attr/colorSurface</item>
4950
<item name="android:elevation" tools:ignore="NewApi">
5051
@dimen/design_bottom_sheet_elevation
5152
</item>
53+
<item name="android:maxWidth">@dimen/material_bottom_sheet_max_width</item>
5254
</style>
5355

5456
<style name="Widget.MaterialComponents.BottomSheet.Modal" parent="Widget.MaterialComponents.BottomSheet">

0 commit comments

Comments
 (0)