Skip to content

Commit

Permalink
[Snackbar] Add shape theming support and update M3 style to use new s…
Browse files Browse the repository at this point in the history
…hapeAppearanceCornerExtraSmall

PiperOrigin-RevId: 444883546
  • Loading branch information
dsn5ft committed Apr 27, 2022
1 parent 64bdf9e commit a717813
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
15 changes: 8 additions & 7 deletions docs/components/Snackbar.md
Expand Up @@ -176,13 +176,14 @@ Element | Attribute | Related method(s) | Default va

### Container attributes

Element | Attribute | Related method(s) | Default value
----------------------- | --------------------------------- | ----------------------------------------------- | -------------
**Color** | `app:backgroundTint` | `setBackgroundTint`<br/>`setBackgroundTintList` | `?attr/colorSurfaceInverse`
**Color overlay alpha** | `app:backgroundOverlayColorAlpha` | N/A | `0.8f` (ignored if `app:backgroundTint` is set)
**Margin** | `android:layout_margin` | N/A | `8dp`
**Elevation** | `app:elevation` | N/A | `6dp`
**Animation mode** | `app:animationMode` | `setAnimationMode`<br/>`getAnimationMode` | `fade`
Element | Attribute | Related method(s) | Default value
----------------------- | ------------------------------------------------------ | ----------------------------------------------- | -------------
**Color** | `app:backgroundTint` | `setBackgroundTint`<br/>`setBackgroundTintList` | `?attr/colorSurfaceInverse`
**Color overlay alpha** | `app:backgroundOverlayColorAlpha` | N/A | `0.8f` (ignored if `app:backgroundTint` is set)
**Shape** | `app:shapeAppearance`<br/>`app:shapeAppearanceOverlay` | N/A | `?attr/shapeAppearanceCornerExtraSmall`
**Margin** | `android:layout_margin` | N/A | `8dp`
**Elevation** | `app:elevation` | N/A | `6dp`
**Animation mode** | `app:animationMode` | `setAnimationMode`<br/>`getAnimationMode` | `fade`

### Action attributes

Expand Down
Expand Up @@ -33,6 +33,7 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.Rect;
Expand Down Expand Up @@ -61,6 +62,7 @@
import android.view.WindowManager;
import android.view.accessibility.AccessibilityManager;
import android.widget.FrameLayout;
import androidx.annotation.ColorInt;
import androidx.annotation.IdRes;
import androidx.annotation.IntDef;
import androidx.annotation.IntRange;
Expand All @@ -81,6 +83,8 @@
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.resources.MaterialResources;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.ShapeAppearanceModel;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -1102,6 +1106,7 @@ public boolean onTouch(View v, MotionEvent event) {
};

@Nullable private BaseTransientBottomBar<?> baseTransientBottomBar;
@Nullable ShapeAppearanceModel shapeAppearanceModel;
@AnimationMode private int animationMode;
private final float backgroundOverlayColorAlpha;
private final float actionTextColorAlpha;
Expand All @@ -1127,6 +1132,13 @@ protected SnackbarBaseLayout(@NonNull Context context, AttributeSet attrs) {
this, a.getDimensionPixelSize(R.styleable.SnackbarLayout_elevation, 0));
}
animationMode = a.getInt(R.styleable.SnackbarLayout_animationMode, ANIMATION_MODE_SLIDE);
if (a.hasValue(R.styleable.SnackbarLayout_shapeAppearance)
|| a.hasValue(R.styleable.SnackbarLayout_shapeAppearanceOverlay)) {
shapeAppearanceModel =
ShapeAppearanceModel.builder(
context, attrs, /* defStyleAttr= */ 0, /* defStyleRes= */ 0)
.build();
}
backgroundOverlayColorAlpha =
a.getFloat(R.styleable.SnackbarLayout_backgroundOverlayColorAlpha, 1);
setBackgroundTintList(
Expand Down Expand Up @@ -1277,17 +1289,15 @@ private void updateOriginalMargins(MarginLayoutParams params) {

@NonNull
private Drawable createThemedBackground() {
float cornerRadius =
getResources().getDimension(R.dimen.mtrl_snackbar_background_corner_radius);

GradientDrawable background = new GradientDrawable();
background.setShape(GradientDrawable.RECTANGLE);
background.setCornerRadius(cornerRadius);

int backgroundColor =
MaterialColors.layer(
this, R.attr.colorSurface, R.attr.colorOnSurface, getBackgroundOverlayColorAlpha());
background.setColor(backgroundColor);
// Only use newer MaterialShapeDrawable background approach if shape appearance is set, in
// order to preserve the original GradientDrawable background for pre-M3 Snackbars.
Drawable background =
shapeAppearanceModel != null
? createMaterialShapeDrawableBackground(backgroundColor, shapeAppearanceModel)
: createGradientDrawableBackground(backgroundColor, getResources());
if (backgroundTint != null) {
Drawable wrappedDrawable = DrawableCompat.wrap(background);
DrawableCompat.setTintList(wrappedDrawable, backgroundTint);
Expand All @@ -1298,6 +1308,25 @@ private Drawable createThemedBackground() {
}
}

@NonNull
private static MaterialShapeDrawable createMaterialShapeDrawableBackground(
@ColorInt int backgroundColor, @NonNull ShapeAppearanceModel shapeAppearanceModel) {
MaterialShapeDrawable background = new MaterialShapeDrawable(shapeAppearanceModel);
background.setFillColor(ColorStateList.valueOf(backgroundColor));
return background;
}

@NonNull
private static GradientDrawable createGradientDrawableBackground(
@ColorInt int backgroundColor, @NonNull Resources resources) {
float cornerRadius = resources.getDimension(R.dimen.mtrl_snackbar_background_corner_radius);
GradientDrawable background = new GradientDrawable();
background.setShape(GradientDrawable.RECTANGLE);
background.setCornerRadius(cornerRadius);
background.setColor(backgroundColor);
return background;
}

/** Behavior for {@link BaseTransientBottomBar}. */
public static class Behavior extends SwipeDismissBehavior<View> {
@NonNull private final BehaviorDelegate delegate;
Expand Down
Expand Up @@ -47,6 +47,12 @@
<!-- Alpha level for the action button text, to allow for adjusting the lightness or darkness of
the theme color used for Snackbar text buttons (e.g., colorPrimary). -->
<attr name="actionTextColorAlpha" format="float"/>
<!-- Shape appearance style reference for Snackbar. Attribute declaration is in the
shape package. -->
<attr name="shapeAppearance" />
<!-- Shape appearance overlay style reference for Snackbar. To be used to augment attributes
declared in the shapeAppearance. Attribute declaration is in the shape package. -->
<attr name="shapeAppearanceOverlay" />
</declare-styleable>

</resources>
Expand Up @@ -80,10 +80,13 @@
<item name="android:background">@null</item>
<item name="android:layout_margin">@dimen/m3_snackbar_margin</item>
<item name="animationMode">fade</item>
<item name="shapeAppearance">?attr/shapeAppearanceCornerExtraSmall</item>
</style>

<!-- Style for a full width M3 Snackbar. -->
<style name="Widget.Material3.Snackbar.FullWidth" parent="Base.Widget.Material3.Snackbar" />
<style name="Widget.Material3.Snackbar.FullWidth" parent="Base.Widget.Material3.Snackbar">
<item name="shapeAppearance">@style/ShapeAppearance.Material3.Corner.None</item>
</style>

<style name="Widget.Material3.Snackbar.TextView" parent="Widget.MaterialComponents.Snackbar.TextView">
<item name="android:alpha">@dimen/m3_snackbar_action_text_color_alpha</item>
Expand Down

0 comments on commit a717813

Please sign in to comment.