Skip to content

Commit

Permalink
[ExtendedFloatingActionButton] Add support for initial show animation.
Browse files Browse the repository at this point in the history
This adds setAnimateShowBeforeLayout() method to override the default behavior. With this change a button which is initially hidden (Visibility.GONE) can be animated in by calling show().

PiperOrigin-RevId: 338508716
  • Loading branch information
Material Design Team authored and dsn5ft committed Oct 22, 2020
1 parent 5d0e032 commit 5c83026
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public class ExtendedFloatingActionButton extends MaterialButton implements Atta

private boolean isExtended = true;
private boolean isTransforming = false;
private boolean animateShowBeforeLayout = false;

@NonNull protected ColorStateList originalTextCsl;

Expand Down Expand Up @@ -318,6 +319,17 @@ public final boolean isExtended() {
return isExtended;
}

/**
* Sets whether to enable animation for a call to show {@link #show} even if the view has not been
* laid out yet.
*
* <p>This may be set to {@code true} if the button is initially hidden but should animate when
* later shown. The default is {@code false}.
*/
public void setAnimateShowBeforeLayout(boolean animateShowBeforeLayout) {
this.animateShowBeforeLayout = animateShowBeforeLayout;
}

@Override
public void setPaddingRelative(int start, int top, int end, int bottom) {
super.setPaddingRelative(start, top, end, bottom);
Expand Down Expand Up @@ -451,7 +463,8 @@ public void hide(@NonNull OnChangedCallback callback) {
/**
* Shows the button.
*
* <p>This method will animate the button show if the view has already been laid out.
* <p>This method will animate the button show if the view has already been laid out, or if {@link
* #setAnimateShowBeforeLayout} is {@code true}.
*/
public void show() {
performMotion(showStrategy, null);
Expand All @@ -460,7 +473,8 @@ public void show() {
/**
* Shows the button.
*
* <p>This method will animate the button show if the view has already been laid out.
* <p>This method will animate the button show if the view has already been laid out, or if {@link
* #setAnimateShowBeforeLayout} is {@code true}.
*
* @param callback the callback to notify when this view is shown
*/
Expand Down Expand Up @@ -683,7 +697,8 @@ private boolean isOrWillBeHidden() {
}

private boolean shouldAnimateVisibilityChange() {
return ViewCompat.isLaidOut(this) && !isInEditMode();
return (ViewCompat.isLaidOut(this) || (!isOrWillBeShown() && animateShowBeforeLayout))
&& !isInEditMode();
}

/**
Expand Down

0 comments on commit 5c83026

Please sign in to comment.