Skip to content

Commit 63f98bf

Browse files
imhappidrchen
authored andcommitted
[BottomAppBar] Small bugs and padding adjustments
resolves #2879 PiperOrigin-RevId: 464840315
1 parent 229eb9d commit 63f98bf

File tree

4 files changed

+67
-26
lines changed

4 files changed

+67
-26
lines changed

catalog/java/io/material/catalog/bottomappbar/BottomAppBarMainDemoFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void onCreate(@Nullable Bundle bundle) {
7171

7272
@Override
7373
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
74-
menuInflater.inflate(R.menu.demo_primary, menu);
74+
menuInflater.inflate(R.menu.demo_primary_alternate, menu);
7575
}
7676

7777
@Override

lib/java/com/google/android/material/bottomappbar/BottomAppBar.java

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,9 @@ private static void updateFabAnchorGravity(BottomAppBar bar, View fab) {
494494
if (bar.fabAnchorMode == FAB_ANCHOR_MODE_CRADLE) {
495495
fabLayoutParams.anchorGravity |= Gravity.TOP;
496496
}
497+
if (bar.fabAnchorMode == FAB_ANCHOR_MODE_EMBED) {
498+
fabLayoutParams.anchorGravity |= Gravity.BOTTOM;
499+
}
497500
}
498501

499502
/**
@@ -1138,7 +1141,15 @@ protected int getActionMenuViewTranslationX(
11381141

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

11431154
return toolbarLeftContentEnd - end;
11441155
}
@@ -1300,26 +1311,32 @@ public void onLayoutChange(
13001311
BottomAppBar child = viewRef.get();
13011312

13021313
// If the child BAB no longer exists, remove the listener.
1303-
if (child == null || !(v instanceof FloatingActionButton)) {
1314+
if (child == null
1315+
|| !(v instanceof FloatingActionButton
1316+
|| v instanceof ExtendedFloatingActionButton)) {
13041317
v.removeOnLayoutChangeListener(this);
13051318
return;
13061319
}
13071320

1308-
FloatingActionButton fab = ((FloatingActionButton) v);
1321+
int height = v.getHeight();
1322+
if (v instanceof FloatingActionButton) {
1323+
FloatingActionButton fab = ((FloatingActionButton) v);
13091324

1310-
fab.getMeasuredContentRect(fabContentRect);
1311-
int height = fabContentRect.height();
1325+
fab.getMeasuredContentRect(fabContentRect);
13121326

1313-
// Set the cutout diameter based on the height of the fab.
1314-
child.setFabDiameter(height);
1327+
height = fabContentRect.height();
13151328

1316-
// Assume symmetrical corners
1317-
float cornerSize =
1318-
fab.getShapeAppearanceModel()
1319-
.getTopLeftCornerSize()
1320-
.getCornerSize(new RectF(fabContentRect));
1329+
// Set the cutout diameter based on the height of the fab.
1330+
child.setFabDiameter(height);
13211331

1322-
child.setFabCornerSize(cornerSize);
1332+
// Assume symmetrical corners
1333+
float cornerSize =
1334+
fab.getShapeAppearanceModel()
1335+
.getTopLeftCornerSize()
1336+
.getCornerSize(new RectF(fabContentRect));
1337+
1338+
child.setFabCornerSize(cornerSize);
1339+
}
13231340

13241341
CoordinatorLayout.LayoutParams fabLayoutParams =
13251342
(CoordinatorLayout.LayoutParams) v.getLayoutParams();
@@ -1329,19 +1346,27 @@ public void onLayoutChange(
13291346
if (originalBottomMargin == 0) {
13301347
// Extra padding is added for the fake shadow on API < 21. Ensure we don't add too
13311348
// much space by removing that extra padding.
1332-
int bottomShadowPadding = (fab.getMeasuredHeight() - height) / 2;
1349+
int bottomShadowPadding = (v.getMeasuredHeight() - height) / 2;
13331350
int bottomMargin = 0;
13341351
if (child.fabAnchorMode == FAB_ANCHOR_MODE_CRADLE) {
1335-
bottomMargin = child
1336-
.getResources()
1337-
.getDimensionPixelOffset(R.dimen.mtrl_bottomappbar_fab_bottom_margin);
1352+
bottomMargin =
1353+
child
1354+
.getResources()
1355+
.getDimensionPixelOffset(R.dimen.mtrl_bottomappbar_fab_bottom_margin);
1356+
// Should be moved above the bottom insets with space ignoring any shadow padding.
1357+
int minBottomMargin = bottomMargin - bottomShadowPadding;
1358+
fabLayoutParams.bottomMargin = child.getBottomInset() + minBottomMargin;
1359+
} else if (child.fabAnchorMode == FAB_ANCHOR_MODE_EMBED) {
1360+
// We want to add a margin of half of the height of the bottom app bar, minus half
1361+
// the height of the fab to the bottom of the fab. Since the height of the bottom
1362+
// app bar does not include the bottom inset, must add it to the height.
1363+
fabLayoutParams.bottomMargin =
1364+
(child.getMeasuredHeight() + child.getBottomInset() - v.getMeasuredHeight())
1365+
/ 2;
13381366
}
1339-
// Should be moved above the bottom insets with space ignoring any shadow padding.
1340-
int minBottomMargin = bottomMargin - bottomShadowPadding;
1341-
fabLayoutParams.bottomMargin = child.getBottomInset() + minBottomMargin;
13421367
fabLayoutParams.leftMargin = child.getLeftInset();
13431368
fabLayoutParams.rightMargin = child.getRightInset();
1344-
boolean isRtl = ViewUtils.isLayoutRtl(fab);
1369+
boolean isRtl = ViewUtils.isLayoutRtl(v);
13451370
if (isRtl) {
13461371
fabLayoutParams.leftMargin += child.fabOffsetEndMode;
13471372
} else {
@@ -1394,12 +1419,11 @@ public boolean onLayoutChild(
13941419
fab.setHideMotionSpecResource(R.animator.mtrl_fab_hide_motion_spec);
13951420
}
13961421

1397-
// Always update the BAB if the fab is laid out.
1398-
fab.addOnLayoutChangeListener(fabLayoutListener);
1399-
14001422
// Ensure the FAB is correctly linked to this BAB so the animations can run correctly
14011423
child.addFabAnimationListeners(fab);
14021424
}
1425+
// Always update the BAB if the fab/efab is laid out.
1426+
dependentView.addOnLayoutChangeListener(fabLayoutListener);
14031427

14041428
// Move the fab to the correct position
14051429
child.setCutoutStateAndTranslateFab();

lib/java/com/google/android/material/bottomappbar/res/values/dimens.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<dimen name="m3_bottomappbar_fab_cradle_rounded_corner_radius">4dp</dimen>
2929
<dimen name="m3_bottomappbar_fab_cradle_vertical_offset">12dp</dimen>
3030
<dimen name="m3_bottomappbar_fab_end_margin">16dp</dimen>
31+
<dimen name="m3_bottomappbar_horizontal_padding">4dp</dimen>
3132

3233
<dimen name="m3_bottomappbar_height">@dimen/m3_comp_bottom_app_bar_container_height</dimen>
3334
</resources>

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,28 @@
7474

7575
<item name="backgroundTint">@macro/m3_comp_bottom_app_bar_container_color</item>
7676
<item name="navigationIconTint">?attr/colorOnSurfaceVariant</item>
77+
<item name="android:paddingLeft">@dimen/m3_bottomappbar_horizontal_padding</item>
78+
<item name="android:paddingStart">@dimen/m3_bottomappbar_horizontal_padding</item>
79+
<item name="android:paddingRight">@dimen/m3_bottomappbar_horizontal_padding</item>
80+
<item name="android:paddingEnd">@dimen/m3_bottomappbar_horizontal_padding</item>
7781
<item name="materialThemeOverlay">@style/ThemeOverlay.Material3.BottomAppBar</item>
7882
</style>
7983

84+
<style name="ThemeOverlay.Material3.BottomAppBar.Legacy" parent="">
85+
<item name="colorControlNormal">?attr/colorOnSurface</item>
86+
<item name="actionMenuTextColor">?attr/colorOnSurface</item>
87+
</style>
88+
8089
<style name="ThemeOverlay.Material3.BottomAppBar" parent="">
8190
<item name="colorControlNormal">?attr/colorOnSurface</item>
8291
<item name="actionMenuTextColor">?attr/colorOnSurface</item>
92+
<item name="toolbarNavigationButtonStyle">@style/Widget.Material3.BottomAppBar.Button.Navigation</item>
93+
</style>
94+
95+
<style name="Widget.Material3.BottomAppBar.Button.Navigation" parent="Widget.AppCompat.Toolbar.Button.Navigation">
96+
<item name="android:minWidth">@dimen/mtrl_min_touch_target_size</item>
97+
<item name="android:scaleType">center</item>
98+
<item name="android:background">?attr/controlBackground</item>
8399
</style>
84100

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

101117
<item name="backgroundTint">@macro/m3_comp_bottom_app_bar_container_color</item>
102118
<item name="navigationIconTint">?attr/colorOnSurfaceVariant</item>
103-
<item name="materialThemeOverlay">@style/ThemeOverlay.Material3.BottomAppBar</item>
119+
<item name="materialThemeOverlay">@style/ThemeOverlay.Material3.BottomAppBar.Legacy</item>
104120
</style>
105121

106122
</resources>

0 commit comments

Comments
 (0)