Skip to content

Commit

Permalink
[BottomAppBar] Add fab end margin attribute
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 456907281
  • Loading branch information
imhappi authored and raajkumars committed Jun 24, 2022
1 parent 397e48f commit 88a73eb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/components/BottomAppBar.md
Expand Up @@ -238,6 +238,7 @@ Element | Attribute | Related
**Cradle margin** | `app:fabCradleMargin` | `setFabCradleMargin`<br>`getFabCradleMargin` | `6dp`
**Cradle rounded corner radius** | `app:fabCradleRoundedCornerRadius` | `setFabCradleRoundedCornerRadius`<br>`getFabCradleRoundedCornerRadius` | `4dp`
**Cradle vertical offset** | `app:fabCradleVerticalOffset` | `setCradleVerticalOffset`<br>`getCradleVerticalOffset` | `12dp`
**End margin** | `app:fabAlignmentModeEndMargin` | `setFabAlignmentModeEndMargin` <br> `getFabAlignmentModeEndMargin` | N/A

See the
[FAB documentation](https://github.com/material-components/material-components-android/tree/master/docs/components/FloatingActionButton.md)
Expand Down
Expand Up @@ -180,15 +180,21 @@ public class BottomAppBar extends Toolbar implements AttachedBehavior {
public @interface MenuAlignmentMode {}

@Nullable private Integer navigationIconTint;
private final int fabOffsetEndMode;
private final MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable();

@Nullable private Animator modeAnimator;
@Nullable private Animator menuAnimator;
@MenuAlignmentMode private int menuAlignmentMode;
@FabAlignmentMode private int fabAlignmentMode;
@FabAnimationMode private int fabAnimationMode;
@FabAnchorMode private int fabAnchorMode;
@MenuAlignmentMode private int menuAlignmentMode;

/** No end margin for the FAB. */
private static final int NO_FAB_END_MARGIN = -1;

private final int fabOffsetEndMode;
@Px private int fabAlignmentModeEndMargin;

private boolean hideOnScroll;
private final boolean paddingBottomSystemWindowInsets;
private final boolean paddingLeftSystemWindowInsets;
Expand Down Expand Up @@ -328,6 +334,9 @@ public BottomAppBar(@NonNull Context context, @Nullable AttributeSet attrs, int
a.getBoolean(R.styleable.BottomAppBar_paddingLeftSystemWindowInsets, false);
paddingRightSystemWindowInsets =
a.getBoolean(R.styleable.BottomAppBar_paddingRightSystemWindowInsets, false);
fabAlignmentModeEndMargin =
a.getDimensionPixelOffset(
R.styleable.BottomAppBar_fabAlignmentModeEndMargin, NO_FAB_END_MARGIN);

a.recycle();

Expand Down Expand Up @@ -600,6 +609,29 @@ public void setCradleVerticalOffset(@Dimension float verticalOffset) {
}
}

/**
* Returns the {@link FloatingActionButton} end margin pixel offset for the fab if it is set.
*
* <p>An end margin of -1 indicates that the default margin will be used.
*/
@Px
public int getFabAlignmentModeEndMargin() {
return fabAlignmentModeEndMargin;
}

/**
* Sets the end margin, in pixels, of the {@link FloatingActionButton}. This will only have an
* effect if the fab alignment mode is {@link #FAB_ALIGNMENT_MODE_END}.
*
* <p>An offset of -1 will use the default margin.
*/
public void setFabAlignmentModeEndMargin(@Px int margin) {
if (fabAlignmentModeEndMargin != margin) {
fabAlignmentModeEndMargin = margin;
setCutoutStateAndTranslateFab();
}
}

/**
* Returns true if the {@link BottomAppBar} should hide when a {@link
* androidx.core.view.NestedScrollingChild} is scrolled. This is handled by {@link
Expand Down Expand Up @@ -994,8 +1026,17 @@ private float getFabTranslationY() {
private float getFabTranslationX(@FabAlignmentMode int fabAlignmentMode) {
boolean isRtl = ViewUtils.isLayoutRtl(this);
if (fabAlignmentMode == FAB_ALIGNMENT_MODE_END) {
View fab = findDependentView();
int systemEndInset = isRtl ? leftInset : rightInset;
int totalEndInset = fabOffsetEndMode + systemEndInset;
int totalEndInset = systemEndInset;
if (fabAlignmentModeEndMargin != NO_FAB_END_MARGIN && fab != null) {
totalEndInset += fab.getMeasuredWidth() / 2 + fabAlignmentModeEndMargin;
} else {
// If no fab end margin is specified, it follows the previous behaviour of
// translating by fabOffsetEndMode instead of a clear-cut margin.
// This will result in a different padding for different FAB sizes.
totalEndInset += fabOffsetEndMode;
}
return (getMeasuredWidth() / 2 - totalEndInset) * (isRtl ? -1 : 1);
} else {
return 0;
Expand Down
Expand Up @@ -22,6 +22,7 @@
<public name="fabCradleMargin" type="attr"/>
<public name="fabCradleRoundedCornerRadius" type="attr"/>
<public name="fabCradleVerticalOffset" type="attr"/>
<public name="fabAlignmentModeEndMargin" type="attr"/>
<public name="menuAlignmentMode" type="attr"/>
<public name="hideOnScroll" type="attr"/>
<public name="Widget.MaterialComponents.BottomAppBar" type="style"/>
Expand Down
Expand Up @@ -50,6 +50,8 @@
<attr name="fabCradleRoundedCornerRadius" format="dimension"/>
<!-- The vertical offset between the fab from the cradle. -->
<attr name="fabCradleVerticalOffset" format="dimension"/>
<!-- The end margin of the fab when the fab is end-aligned. -->
<attr name="fabAlignmentModeEndMargin" format="dimension"/>
<!-- 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

0 comments on commit 88a73eb

Please sign in to comment.