Skip to content

Commit 13dbc9f

Browse files
drchenraajkumars
authored andcommitted
[BottomSheet] Support background tint without shape appearance set
Resolves #2200 PiperOrigin-RevId: 428781539
1 parent 85a4405 commit 13dbc9f

File tree

2 files changed

+28
-41
lines changed

2 files changed

+28
-41
lines changed

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

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,10 @@ public abstract static class BottomSheetCallback {
217217
/** Peek height gesture inset buffer to ensure enough swipeable space. */
218218
private int peekHeightGestureInsetBuffer;
219219

220-
/** True if Behavior has a non-null value for the @shapeAppearance attribute */
221-
private boolean shapeThemingEnabled;
222-
223220
private MaterialShapeDrawable materialShapeDrawable;
224221

222+
@Nullable private ColorStateList backgroundTint;
223+
225224
private int maxWidth = NO_MAX_SIZE;
226225

227226
private int maxHeight = NO_MAX_SIZE;
@@ -311,16 +310,16 @@ public BottomSheetBehavior(@NonNull Context context, @Nullable AttributeSet attr
311310
context.getResources().getDimensionPixelSize(R.dimen.mtrl_min_touch_target_size);
312311

313312
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BottomSheetBehavior_Layout);
314-
this.shapeThemingEnabled = a.hasValue(R.styleable.BottomSheetBehavior_Layout_shapeAppearance);
315-
boolean hasBackgroundTint = a.hasValue(R.styleable.BottomSheetBehavior_Layout_backgroundTint);
316-
if (hasBackgroundTint) {
317-
ColorStateList bottomSheetColor =
318-
MaterialResources.getColorStateList(
319-
context, a, R.styleable.BottomSheetBehavior_Layout_backgroundTint);
320-
createMaterialShapeDrawable(context, attrs, hasBackgroundTint, bottomSheetColor);
321-
} else {
322-
createMaterialShapeDrawable(context, attrs, hasBackgroundTint);
313+
if (a.hasValue(R.styleable.BottomSheetBehavior_Layout_backgroundTint)) {
314+
this.backgroundTint = MaterialResources.getColorStateList(
315+
context, a, R.styleable.BottomSheetBehavior_Layout_backgroundTint);
316+
}
317+
if (a.hasValue(R.styleable.BottomSheetBehavior_Layout_shapeAppearance)) {
318+
this.shapeAppearanceModelDefault =
319+
ShapeAppearanceModel.builder(context, attrs, R.attr.bottomSheetStyle, DEF_STYLE_RES)
320+
.build();
323321
}
322+
createMaterialShapeDrawableIfNeeded(context);
324323
createShapeValueAnimator();
325324

326325
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
@@ -499,17 +498,16 @@ public boolean onLayoutChild(
499498
viewRef = new WeakReference<>(child);
500499
// Only set MaterialShapeDrawable as background if shapeTheming is enabled, otherwise will
501500
// default to android:background declared in styles or layout.
502-
if (shapeThemingEnabled && materialShapeDrawable != null) {
503-
ViewCompat.setBackground(child, materialShapeDrawable);
504-
}
505-
// Set elevation on MaterialShapeDrawable
506501
if (materialShapeDrawable != null) {
502+
ViewCompat.setBackground(child, materialShapeDrawable);
507503
// Use elevation attr if set on bottomsheet; otherwise, use elevation of child view.
508504
materialShapeDrawable.setElevation(
509505
elevation == -1 ? ViewCompat.getElevation(child) : elevation);
510506
// Update the material shape based on initial state.
511507
isShapeExpanded = state == STATE_EXPANDED;
512508
materialShapeDrawable.setInterpolation(isShapeExpanded ? 0f : 1f);
509+
} else if (backgroundTint != null) {
510+
ViewCompat.setBackgroundTintList(child, backgroundTint);
513511
}
514512
updateAccessibilityActions();
515513
if (ViewCompat.getImportantForAccessibility(child)
@@ -1411,32 +1409,21 @@ private boolean shouldHandleDraggingWithHelper() {
14111409
return viewDragHelper != null && (draggable || state == STATE_DRAGGING);
14121410
}
14131411

1414-
private void createMaterialShapeDrawable(
1415-
@NonNull Context context, AttributeSet attrs, boolean hasBackgroundTint) {
1416-
this.createMaterialShapeDrawable(context, attrs, hasBackgroundTint, null);
1417-
}
1418-
1419-
private void createMaterialShapeDrawable(
1420-
@NonNull Context context,
1421-
AttributeSet attrs,
1422-
boolean hasBackgroundTint,
1423-
@Nullable ColorStateList bottomSheetColor) {
1424-
if (this.shapeThemingEnabled) {
1425-
this.shapeAppearanceModelDefault =
1426-
ShapeAppearanceModel.builder(context, attrs, R.attr.bottomSheetStyle, DEF_STYLE_RES)
1427-
.build();
1412+
private void createMaterialShapeDrawableIfNeeded(@NonNull Context context) {
1413+
if (shapeAppearanceModelDefault == null) {
1414+
return;
1415+
}
14281416

1429-
this.materialShapeDrawable = new MaterialShapeDrawable(shapeAppearanceModelDefault);
1430-
this.materialShapeDrawable.initializeElevationOverlay(context);
1417+
this.materialShapeDrawable = new MaterialShapeDrawable(shapeAppearanceModelDefault);
1418+
this.materialShapeDrawable.initializeElevationOverlay(context);
14311419

1432-
if (hasBackgroundTint && bottomSheetColor != null) {
1433-
materialShapeDrawable.setFillColor(bottomSheetColor);
1434-
} else {
1435-
// If the tint isn't set, use the theme default background color.
1436-
TypedValue defaultColor = new TypedValue();
1437-
context.getTheme().resolveAttribute(android.R.attr.colorBackground, defaultColor, true);
1438-
materialShapeDrawable.setTint(defaultColor.data);
1439-
}
1420+
if (backgroundTint != null) {
1421+
materialShapeDrawable.setFillColor(backgroundTint);
1422+
} else {
1423+
// If the tint isn't set, use the theme default background color.
1424+
TypedValue defaultColor = new TypedValue();
1425+
context.getTheme().resolveAttribute(android.R.attr.colorBackground, defaultColor, true);
1426+
materialShapeDrawable.setTint(defaultColor.data);
14401427
}
14411428
}
14421429

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<item name="behavior_skipCollapsed">false</item>
3838
<item name="shapeAppearance">@null</item>
3939
<item name="shapeAppearanceOverlay">@null</item>
40-
<item name="backgroundTint">?android:attr/colorBackground</item>
40+
<item name="backgroundTint">@null</item>
4141
</style>
4242

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

0 commit comments

Comments
 (0)