Skip to content

Commit

Permalink
[BottomSheet] Support background tint without shape appearance set
Browse files Browse the repository at this point in the history
Resolves #2200

PiperOrigin-RevId: 428781539
  • Loading branch information
drchen authored and raajkumars committed Feb 15, 2022
1 parent 85a4405 commit 13dbc9f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 41 deletions.
Expand Up @@ -217,11 +217,10 @@ public abstract static class BottomSheetCallback {
/** Peek height gesture inset buffer to ensure enough swipeable space. */
private int peekHeightGestureInsetBuffer;

/** True if Behavior has a non-null value for the @shapeAppearance attribute */
private boolean shapeThemingEnabled;

private MaterialShapeDrawable materialShapeDrawable;

@Nullable private ColorStateList backgroundTint;

private int maxWidth = NO_MAX_SIZE;

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

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BottomSheetBehavior_Layout);
this.shapeThemingEnabled = a.hasValue(R.styleable.BottomSheetBehavior_Layout_shapeAppearance);
boolean hasBackgroundTint = a.hasValue(R.styleable.BottomSheetBehavior_Layout_backgroundTint);
if (hasBackgroundTint) {
ColorStateList bottomSheetColor =
MaterialResources.getColorStateList(
context, a, R.styleable.BottomSheetBehavior_Layout_backgroundTint);
createMaterialShapeDrawable(context, attrs, hasBackgroundTint, bottomSheetColor);
} else {
createMaterialShapeDrawable(context, attrs, hasBackgroundTint);
if (a.hasValue(R.styleable.BottomSheetBehavior_Layout_backgroundTint)) {
this.backgroundTint = MaterialResources.getColorStateList(
context, a, R.styleable.BottomSheetBehavior_Layout_backgroundTint);
}
if (a.hasValue(R.styleable.BottomSheetBehavior_Layout_shapeAppearance)) {
this.shapeAppearanceModelDefault =
ShapeAppearanceModel.builder(context, attrs, R.attr.bottomSheetStyle, DEF_STYLE_RES)
.build();
}
createMaterialShapeDrawableIfNeeded(context);
createShapeValueAnimator();

if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
Expand Down Expand Up @@ -499,17 +498,16 @@ public boolean onLayoutChild(
viewRef = new WeakReference<>(child);
// Only set MaterialShapeDrawable as background if shapeTheming is enabled, otherwise will
// default to android:background declared in styles or layout.
if (shapeThemingEnabled && materialShapeDrawable != null) {
ViewCompat.setBackground(child, materialShapeDrawable);
}
// Set elevation on MaterialShapeDrawable
if (materialShapeDrawable != null) {
ViewCompat.setBackground(child, materialShapeDrawable);
// Use elevation attr if set on bottomsheet; otherwise, use elevation of child view.
materialShapeDrawable.setElevation(
elevation == -1 ? ViewCompat.getElevation(child) : elevation);
// Update the material shape based on initial state.
isShapeExpanded = state == STATE_EXPANDED;
materialShapeDrawable.setInterpolation(isShapeExpanded ? 0f : 1f);
} else if (backgroundTint != null) {
ViewCompat.setBackgroundTintList(child, backgroundTint);
}
updateAccessibilityActions();
if (ViewCompat.getImportantForAccessibility(child)
Expand Down Expand Up @@ -1411,32 +1409,21 @@ private boolean shouldHandleDraggingWithHelper() {
return viewDragHelper != null && (draggable || state == STATE_DRAGGING);
}

private void createMaterialShapeDrawable(
@NonNull Context context, AttributeSet attrs, boolean hasBackgroundTint) {
this.createMaterialShapeDrawable(context, attrs, hasBackgroundTint, null);
}

private void createMaterialShapeDrawable(
@NonNull Context context,
AttributeSet attrs,
boolean hasBackgroundTint,
@Nullable ColorStateList bottomSheetColor) {
if (this.shapeThemingEnabled) {
this.shapeAppearanceModelDefault =
ShapeAppearanceModel.builder(context, attrs, R.attr.bottomSheetStyle, DEF_STYLE_RES)
.build();
private void createMaterialShapeDrawableIfNeeded(@NonNull Context context) {
if (shapeAppearanceModelDefault == null) {
return;
}

this.materialShapeDrawable = new MaterialShapeDrawable(shapeAppearanceModelDefault);
this.materialShapeDrawable.initializeElevationOverlay(context);
this.materialShapeDrawable = new MaterialShapeDrawable(shapeAppearanceModelDefault);
this.materialShapeDrawable.initializeElevationOverlay(context);

if (hasBackgroundTint && bottomSheetColor != null) {
materialShapeDrawable.setFillColor(bottomSheetColor);
} else {
// If the tint isn't set, use the theme default background color.
TypedValue defaultColor = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.colorBackground, defaultColor, true);
materialShapeDrawable.setTint(defaultColor.data);
}
if (backgroundTint != null) {
materialShapeDrawable.setFillColor(backgroundTint);
} else {
// If the tint isn't set, use the theme default background color.
TypedValue defaultColor = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.colorBackground, defaultColor, true);
materialShapeDrawable.setTint(defaultColor.data);
}
}

Expand Down
Expand Up @@ -37,7 +37,7 @@
<item name="behavior_skipCollapsed">false</item>
<item name="shapeAppearance">@null</item>
<item name="shapeAppearanceOverlay">@null</item>
<item name="backgroundTint">?android:attr/colorBackground</item>
<item name="backgroundTint">@null</item>
</style>

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

0 comments on commit 13dbc9f

Please sign in to comment.