Skip to content

Commit

Permalink
[BottomAppBar] Small bugs and padding adjustments
Browse files Browse the repository at this point in the history
resolves #2879

PiperOrigin-RevId: 464840315
  • Loading branch information
imhappi authored and drchen committed Aug 4, 2022
1 parent 229eb9d commit 63f98bf
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 26 deletions.
Expand Up @@ -71,7 +71,7 @@ public void onCreate(@Nullable Bundle bundle) {

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.demo_primary, menu);
menuInflater.inflate(R.menu.demo_primary_alternate, menu);
}

@Override
Expand Down
72 changes: 48 additions & 24 deletions lib/java/com/google/android/material/bottomappbar/BottomAppBar.java
Expand Up @@ -494,6 +494,9 @@ private static void updateFabAnchorGravity(BottomAppBar bar, View fab) {
if (bar.fabAnchorMode == FAB_ANCHOR_MODE_CRADLE) {
fabLayoutParams.anchorGravity |= Gravity.TOP;
}
if (bar.fabAnchorMode == FAB_ANCHOR_MODE_EMBED) {
fabLayoutParams.anchorGravity |= Gravity.BOTTOM;
}
}

/**
Expand Down Expand Up @@ -1138,7 +1141,15 @@ protected int getActionMenuViewTranslationX(

int actionMenuViewStart = isRtl ? actionMenuView.getRight() : actionMenuView.getLeft();
int systemStartInset = isRtl ? rightInset : -leftInset;
int end = actionMenuViewStart + systemStartInset;
// If there's no navigation icon, we want to add margin since we are translating the menu items
// to the start.
int marginStart = 0;
if (getNavigationIcon() == null) {
int horizontalMargin =
getResources().getDimensionPixelOffset(R.dimen.m3_bottomappbar_horizontal_padding);
marginStart = isRtl ? horizontalMargin : -horizontalMargin;
}
int end = actionMenuViewStart + systemStartInset + marginStart;

return toolbarLeftContentEnd - end;
}
Expand Down Expand Up @@ -1300,26 +1311,32 @@ public void onLayoutChange(
BottomAppBar child = viewRef.get();

// If the child BAB no longer exists, remove the listener.
if (child == null || !(v instanceof FloatingActionButton)) {
if (child == null
|| !(v instanceof FloatingActionButton
|| v instanceof ExtendedFloatingActionButton)) {
v.removeOnLayoutChangeListener(this);
return;
}

FloatingActionButton fab = ((FloatingActionButton) v);
int height = v.getHeight();
if (v instanceof FloatingActionButton) {
FloatingActionButton fab = ((FloatingActionButton) v);

fab.getMeasuredContentRect(fabContentRect);
int height = fabContentRect.height();
fab.getMeasuredContentRect(fabContentRect);

// Set the cutout diameter based on the height of the fab.
child.setFabDiameter(height);
height = fabContentRect.height();

// Assume symmetrical corners
float cornerSize =
fab.getShapeAppearanceModel()
.getTopLeftCornerSize()
.getCornerSize(new RectF(fabContentRect));
// Set the cutout diameter based on the height of the fab.
child.setFabDiameter(height);

child.setFabCornerSize(cornerSize);
// Assume symmetrical corners
float cornerSize =
fab.getShapeAppearanceModel()
.getTopLeftCornerSize()
.getCornerSize(new RectF(fabContentRect));

child.setFabCornerSize(cornerSize);
}

CoordinatorLayout.LayoutParams fabLayoutParams =
(CoordinatorLayout.LayoutParams) v.getLayoutParams();
Expand All @@ -1329,19 +1346,27 @@ public void onLayoutChange(
if (originalBottomMargin == 0) {
// Extra padding is added for the fake shadow on API < 21. Ensure we don't add too
// much space by removing that extra padding.
int bottomShadowPadding = (fab.getMeasuredHeight() - height) / 2;
int bottomShadowPadding = (v.getMeasuredHeight() - height) / 2;
int bottomMargin = 0;
if (child.fabAnchorMode == FAB_ANCHOR_MODE_CRADLE) {
bottomMargin = child
.getResources()
.getDimensionPixelOffset(R.dimen.mtrl_bottomappbar_fab_bottom_margin);
bottomMargin =
child
.getResources()
.getDimensionPixelOffset(R.dimen.mtrl_bottomappbar_fab_bottom_margin);
// Should be moved above the bottom insets with space ignoring any shadow padding.
int minBottomMargin = bottomMargin - bottomShadowPadding;
fabLayoutParams.bottomMargin = child.getBottomInset() + minBottomMargin;
} else if (child.fabAnchorMode == FAB_ANCHOR_MODE_EMBED) {
// We want to add a margin of half of the height of the bottom app bar, minus half
// the height of the fab to the bottom of the fab. Since the height of the bottom
// app bar does not include the bottom inset, must add it to the height.
fabLayoutParams.bottomMargin =
(child.getMeasuredHeight() + child.getBottomInset() - v.getMeasuredHeight())
/ 2;
}
// Should be moved above the bottom insets with space ignoring any shadow padding.
int minBottomMargin = bottomMargin - bottomShadowPadding;
fabLayoutParams.bottomMargin = child.getBottomInset() + minBottomMargin;
fabLayoutParams.leftMargin = child.getLeftInset();
fabLayoutParams.rightMargin = child.getRightInset();
boolean isRtl = ViewUtils.isLayoutRtl(fab);
boolean isRtl = ViewUtils.isLayoutRtl(v);
if (isRtl) {
fabLayoutParams.leftMargin += child.fabOffsetEndMode;
} else {
Expand Down Expand Up @@ -1394,12 +1419,11 @@ public boolean onLayoutChild(
fab.setHideMotionSpecResource(R.animator.mtrl_fab_hide_motion_spec);
}

// Always update the BAB if the fab is laid out.
fab.addOnLayoutChangeListener(fabLayoutListener);

// Ensure the FAB is correctly linked to this BAB so the animations can run correctly
child.addFabAnimationListeners(fab);
}
// Always update the BAB if the fab/efab is laid out.
dependentView.addOnLayoutChangeListener(fabLayoutListener);

// Move the fab to the correct position
child.setCutoutStateAndTranslateFab();
Expand Down
Expand Up @@ -28,6 +28,7 @@
<dimen name="m3_bottomappbar_fab_cradle_rounded_corner_radius">4dp</dimen>
<dimen name="m3_bottomappbar_fab_cradle_vertical_offset">12dp</dimen>
<dimen name="m3_bottomappbar_fab_end_margin">16dp</dimen>
<dimen name="m3_bottomappbar_horizontal_padding">4dp</dimen>

<dimen name="m3_bottomappbar_height">@dimen/m3_comp_bottom_app_bar_container_height</dimen>
</resources>
Expand Up @@ -74,12 +74,28 @@

<item name="backgroundTint">@macro/m3_comp_bottom_app_bar_container_color</item>
<item name="navigationIconTint">?attr/colorOnSurfaceVariant</item>
<item name="android:paddingLeft">@dimen/m3_bottomappbar_horizontal_padding</item>
<item name="android:paddingStart">@dimen/m3_bottomappbar_horizontal_padding</item>
<item name="android:paddingRight">@dimen/m3_bottomappbar_horizontal_padding</item>
<item name="android:paddingEnd">@dimen/m3_bottomappbar_horizontal_padding</item>
<item name="materialThemeOverlay">@style/ThemeOverlay.Material3.BottomAppBar</item>
</style>

<style name="ThemeOverlay.Material3.BottomAppBar.Legacy" parent="">
<item name="colorControlNormal">?attr/colorOnSurface</item>
<item name="actionMenuTextColor">?attr/colorOnSurface</item>
</style>

<style name="ThemeOverlay.Material3.BottomAppBar" parent="">
<item name="colorControlNormal">?attr/colorOnSurface</item>
<item name="actionMenuTextColor">?attr/colorOnSurface</item>
<item name="toolbarNavigationButtonStyle">@style/Widget.Material3.BottomAppBar.Button.Navigation</item>
</style>

<style name="Widget.Material3.BottomAppBar.Button.Navigation" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="android:minWidth">@dimen/mtrl_min_touch_target_size</item>
<item name="android:scaleType">center</item>
<item name="android:background">?attr/controlBackground</item>
</style>

<style name="Widget.Material3.BottomAppBar.Legacy" parent="Widget.MaterialComponents.BottomAppBar">
Expand All @@ -100,7 +116,7 @@

<item name="backgroundTint">@macro/m3_comp_bottom_app_bar_container_color</item>
<item name="navigationIconTint">?attr/colorOnSurfaceVariant</item>
<item name="materialThemeOverlay">@style/ThemeOverlay.Material3.BottomAppBar</item>
<item name="materialThemeOverlay">@style/ThemeOverlay.Material3.BottomAppBar.Legacy</item>
</style>

</resources>

0 comments on commit 63f98bf

Please sign in to comment.