Skip to content

Commit

Permalink
[BottomAppBar] Add attribute to remove shadow on container
Browse files Browse the repository at this point in the history
resolves #2953

PiperOrigin-RevId: 476451267
  • Loading branch information
imhappi authored and pekingme committed Sep 26, 2022
1 parent 11e98f2 commit 1322e61
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
11 changes: 6 additions & 5 deletions docs/components/BottomAppBar.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,12 @@ floating action button (FAB), action item(s) and an overflow menu.

### Container attributes

Element | Attribute | Related method(s) | Default value
------------- | -------------------- | ------------------------------------------ | -------------
**Color** | `app:backgroundTint` | `setBackgroundTint`<br>`getBackgroundTint` | `?attr/colorSurface`
**Elevation** | `app:elevation` | `setElevation` | `3dp`
**Height** | `android:minHeight` | `setMinimumHeight`<br>`getMinimumHeight` | `80dp`
Element | Attribute | Related method(s) | Default value
------------- | ------------------------ | ------------------------------------------ | -------------
**Color** | `app:backgroundTint` | `setBackgroundTint`<br>`getBackgroundTint` | `?attr/colorSurface`
**Elevation** | `app:elevation` | `setElevation` | `3dp`
**Height** | `android:minHeight` | `setMinimumHeight`<br>`getMinimumHeight` | `80dp`
**Shadows** | `app:addElevationShadow` | N/A | `false`

### Navigation icon attributes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.google.android.material.R;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import static com.google.android.material.shape.MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS;
import static com.google.android.material.shape.MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER;
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;

import android.animation.Animator;
Expand All @@ -29,10 +29,13 @@
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.appcompat.widget.ActionMenuView;
Expand Down Expand Up @@ -342,6 +345,7 @@ public BottomAppBar(@NonNull Context context, @Nullable AttributeSet attrs, int
a.getDimensionPixelOffset(
R.styleable.BottomAppBar_fabAlignmentModeEndMargin, NO_FAB_END_MARGIN);

boolean addElevationShadow = a.getBoolean(R.styleable.BottomAppBar_addElevationShadow, true);
a.recycle();

fabOffsetEndMode =
Expand All @@ -352,7 +356,16 @@ public BottomAppBar(@NonNull Context context, @Nullable AttributeSet attrs, int
ShapeAppearanceModel shapeAppearanceModel =
ShapeAppearanceModel.builder().setTopEdge(topEdgeTreatment).build();
materialShapeDrawable.setShapeAppearanceModel(shapeAppearanceModel);
materialShapeDrawable.setShadowCompatibilityMode(SHADOW_COMPAT_MODE_ALWAYS);
if (addElevationShadow) {
materialShapeDrawable.setShadowCompatibilityMode(
MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS);
} else {
materialShapeDrawable.setShadowCompatibilityMode(SHADOW_COMPAT_MODE_NEVER);
if (VERSION.SDK_INT >= VERSION_CODES.P) {
setOutlineAmbientShadowColor(Color.TRANSPARENT);
setOutlineSpotShadowColor(Color.TRANSPARENT);
}
}
materialShapeDrawable.setPaintStyle(Style.FILL);
materialShapeDrawable.initializeElevationOverlay(context);
setElevation(elevation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<public name="menuAlignmentMode" type="attr"/>
<public name="hideOnScroll" type="attr"/>
<public name="removeEmbeddedFabElevation" type="attr"/>
<public name="addElevationShadow" type="attr"/>
<public name="Widget.MaterialComponents.BottomAppBar" type="style"/>
<public name="Widget.MaterialComponents.BottomAppBar.Colored" type="style"/>
<public name="Widget.MaterialComponents.BottomAppBar.PrimarySurface" type="style"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<attr name="fabAlignmentModeEndMargin" format="dimension"/>
<!-- Whether or not to remove elevation for embedded fabs in the bottom app bar. -->
<attr name="removeEmbeddedFabElevation" format="boolean"/>
<!-- Whether or not to add shadows for the bottom app bar. -->
<attr name="addElevationShadow" format="boolean"/>
<!-- Whether the BottomAppBar should hide when a NestedScrollView is scrolled. -->
<attr name="hideOnScroll" format="boolean"/>
<!-- Whether the BottomAppBar should apply padding to be above the bottom window insets. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">

<style name="Widget.MaterialComponents.BottomAppBar" parent="Widget.AppCompat.Toolbar">
<item name="enforceMaterialTheme">true</item>
Expand All @@ -33,6 +33,7 @@
<item name="fabAnchorMode">cradle</item>
<item name="fabAlignmentMode">center</item>
<item name="menuAlignmentMode">auto</item>
<item name="addElevationShadow">true</item>
</style>

<style name="Widget.MaterialComponents.BottomAppBar.Colored" parent="Widget.MaterialComponents.BottomAppBar">
Expand Down Expand Up @@ -71,6 +72,7 @@
@dimen/m3_bottomappbar_fab_cradle_vertical_offset
</item>
<item name="elevation">@dimen/m3_comp_bottom_app_bar_container_elevation</item>
<item name="addElevationShadow">false</item>

<item name="backgroundTint">@macro/m3_comp_bottom_app_bar_container_color</item>
<item name="navigationIconTint">?attr/colorOnSurfaceVariant</item>
Expand Down

0 comments on commit 1322e61

Please sign in to comment.