FloatingActionButton

public class FloatingActionButton implements ExpandableTransformationWidget, Shapeable


Floating action buttons are used for a special type of promoted action. They are distinguished by a circled icon floating above the UI and have special motion behaviors related to morphing, launching, and the transferring anchor point.

Floating action buttons come in two sizes: the default and the mini. The size can be controlled with the fabSize attribute.

As this class descends from ImageView, you can control the icon which is displayed via setImageDrawable.

The background color of this view defaults to the your theme's colorSecondary. If you wish to change this at runtime then you can do so via setBackgroundTintList.

For more information, see the component developer guidance and design guidelines.

Summary

Nested types

Behavior designed for use with FloatingActionButton instances.

Behavior designed for use with FloatingActionButton instances.

Callback to be invoked when the visibility of a FloatingActionButton changes.

Constants

static final int

Indicates that the FloatingActionButton should not have a custom size, and instead that the size should be calculated based on the value set using setSize or the fabSize attribute.

static final int

Size which will change based on the window size.

static final int

The mini sized button, 40dp.

static final int

The normal sized button, 56dp.

Public fields

Mode
int
int
int

Public constructors

FloatingActionButton(Context context)
FloatingActionButton(Context context, AttributeSet attrs)
FloatingActionButton(
    Context context,
    AttributeSet attrs,
    int defStyleAttr
)

Public methods

void
addOnHideAnimationListener(AnimatorListener listener)
void
addOnShowAnimationListener(AnimatorListener listener)
void

Add a TransformationCallback which can watch for changes to this view.

void

Clears the custom size for this FloatingActionButton.

CharSequence
ColorStateList

Returns the tint applied to the background drawable, if specified.

Mode

Returns the blending mode used to apply the tint to the background drawable, if specified.

Behavior<FloatingActionButton>
float

Returns the backward compatible elevation of the FloatingActionButton.

float

Returns the backward compatible hovered/focused translationZ of the FloatingActionButton.

float

Returns the backward compatible pressed translationZ of the FloatingActionButton.

Drawable

Returns the FloatingActionButton's background, minus any compatible shadow implementation.

boolean
getContentRect(Rect rect)

This method is deprecated.

prefer getMeasuredContentRect instead, so you don't need to handle the case where the view isn't laid out.

int

Returns the custom size for this FloatingActionButton.

int

Returns the expanded component id hint.

MotionSpec

Returns the motion spec for the hide animation.

void

Return in rect the bounds of the actual floating action button content in view-local coordinates.

int

This method is deprecated.

Use getRippleColorStateList instead.

ColorStateList

Returns the ripple color for this button.

ShapeAppearanceModel

Returns the ShapeAppearanceModel for this FloatingActionButton.

MotionSpec

Returns the motion spec for the show animation.

int

Returns the chosen size for this button.

ColorStateList

Compat method to support TintableBackgroundView.

Mode

Compat method to support TintableBackgroundView.

ColorStateList
Mode
boolean

Returns whether FloatingActionButton will add inner padding on platforms Lollipop and after.

final int
void

Hides the button.

void

Hides the button.

final void
internalSetVisibility(int visibility, boolean fromUser)
boolean

Returns whether this widget is expanded.

boolean
boolean
void
boolean
onTouchEvent(MotionEvent ev)
void
removeOnHideAnimationListener(AnimatorListener listener)
void
removeOnShowAnimationListener(AnimatorListener listener)
void

Remove the TransformationCallback from this view.

void
setBackgroundColor(int color)
void
setBackgroundDrawable(Drawable background)
void
void
setBackgroundTintList(ColorStateList tint)

Applies a tint to the background drawable.

void
setBackgroundTintMode(Mode tintMode)

Specifies the blending mode used to apply the tint specified by setBackgroundTintList} to the background drawable.

void
setCompatElevation(float elevation)

Updates the backward compatible elevation of the FloatingActionButton.

void

Updates the backward compatible elevation of the FloatingActionButton.

void

Updates the backward compatible hovered/focused translationZ of the FloatingActionButton.

void

Updates the backward compatible hovered/focused translationZ of the FloatingActionButton.

void
setCompatPressedTranslationZ(float translationZ)

Updates the backward compatible pressed translationZ of the FloatingActionButton.

void

Updates the backward compatible pressed translationZ of the FloatingActionButton.

void
setCustomSize(int size)

Sets the size of the button to be a custom value in pixels.

void
setElevation(float elevation)
void

Sets whether this FloatingActionButton should expand its bounds (if needed) to meet the minimum touch target size.

boolean
setExpanded(boolean expanded)

Sets the expanded state on this widget.

void
setExpandedComponentIdHint(int expandedComponentIdHint)

Sets the expanded component id hint, which may be used by a Behavior to determine whether it should handle this widget's state change.

void

Updates the motion spec for the hide animation.

void

Updates the motion spec for the hide animation.

void
setImageDrawable(Drawable drawable)
void
setImageResource(int resId)
void
setMaxImageSize(int imageSize)

Sets the max image size for this button.

void
setRippleColor(ColorStateList color)

Sets the ripple color for this button.

void
setRippleColor(int color)

Sets the ripple color for this button.

void
setScaleX(float scaleX)
void
setScaleY(float scaleY)
void

Sets the ShapeAppearanceModel for this FloatingActionButton.

void

Updates the motion spec for the show animation.

void

Updates the motion spec for the show animation.

void
setSize(int size)

Sets the size of the button.

void
setSupportBackgroundTintList(ColorStateList tint)

Compat method to support TintableBackgroundView.

void

Compat method to support TintableBackgroundView.

void
setSupportImageTintList(ColorStateList tint)
void
setSupportImageTintMode(Mode tintMode)
void
setTranslationX(float translationX)
void
setTranslationY(float translationY)
void
setTranslationZ(float translationZ)
void
setUseCompatPadding(boolean useCompatPadding)

Set whether FloatingActionButton should add inner padding on platforms Lollipop and after, to ensure consistent dimensions on all platforms.

void
setVisibility(int visibility)
boolean

Returns whether this fab will expand its bounds (if needed) to meet the minimum touch target size.

void

Shows the button.

void

Shows the button.

Protected methods

void
void
void
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void
onRestoreInstanceState(Parcelable state)
Parcelable

Constants

NO_CUSTOM_SIZE

public static final int NO_CUSTOM_SIZE = 0

Indicates that the FloatingActionButton should not have a custom size, and instead that the size should be calculated based on the value set using setSize or the fabSize attribute. Instead of using this constant directly, you can call the clearCustomSize method.

SIZE_AUTO

public static final int SIZE_AUTO = -1

Size which will change based on the window size. For small sized windows (largest screen dimension <470dp) this will select a mini sized button (SIZE_MINI), and for larger sized windows it will select a normal sized button (SIZE_NORMAL).

See also
setSize

SIZE_MINI

public static final int SIZE_MINI = 1

The mini sized button, 40dp. Will always be smaller than SIZE_NORMAL.

See also
setSize

SIZE_NORMAL

public static final int SIZE_NORMAL = 0

The normal sized button, 56dp. Will always be larger than SIZE_MINI.

See also
setSize

Public fields

backgroundTintMode

public Mode backgroundTintMode

customSize

public int customSize

size

public int size

userSetVisibility

public int userSetVisibility

Public constructors

FloatingActionButton

public FloatingActionButton(Context context)

FloatingActionButton

public FloatingActionButton(Context context, AttributeSet attrs)

FloatingActionButton

public FloatingActionButton(
    Context context,
    AttributeSet attrs,
    int defStyleAttr
)

Public methods

addOnHideAnimationListener

public void addOnHideAnimationListener(AnimatorListener listener)

addOnShowAnimationListener

public void addOnShowAnimationListener(AnimatorListener listener)

addTransformationCallback

public void addTransformationCallback(
    TransformationCallback<FloatingActionButton> listener
)

Add a TransformationCallback which can watch for changes to this view.

clearCustomSize

public void clearCustomSize()

Clears the custom size for this FloatingActionButton.

If called, custom sizing will not be used and the size will be calculated based on the value set using setSize or the fabSize attribute

getAccessibilityClassName

public CharSequence getAccessibilityClassName()

getBackgroundTintList

public ColorStateList getBackgroundTintList()

Returns the tint applied to the background drawable, if specified.

Returns
ColorStateList

the tint applied to the background drawable

getBackgroundTintMode

public Mode getBackgroundTintMode()

Returns the blending mode used to apply the tint to the background drawable, if specified.

Returns
Mode

the blending mode used to apply the tint to the background drawable

getBehavior

public Behavior<FloatingActionButton> getBehavior()

getCompatElevation

public float getCompatElevation()

Returns the backward compatible elevation of the FloatingActionButton.

ref com.google.android.material.R.styleable#FloatingActionButton_elevation

Returns
float

the backward compatible elevation in pixels.

getCompatHoveredFocusedTranslationZ

public float getCompatHoveredFocusedTranslationZ()

Returns the backward compatible hovered/focused translationZ of the FloatingActionButton.

ref com.google.android.material.R.styleable#FloatingActionButton_hoveredFocusedTranslationZ

Returns
float

the backward compatible hovered/focused translationZ in pixels.

getCompatPressedTranslationZ

public float getCompatPressedTranslationZ()

Returns the backward compatible pressed translationZ of the FloatingActionButton.

ref com.google.android.material.R.styleable#FloatingActionButton_pressedTranslationZ

Returns
float

the backward compatible pressed translationZ in pixels.

getContentBackground

public Drawable getContentBackground()

Returns the FloatingActionButton's background, minus any compatible shadow implementation.

getContentRect

public boolean getContentRect(Rect rect)

Return in rect the bounds of the actual floating action button content in view-local coordinates. This is defined as anything within any visible shadow.

Returns
boolean

true if this view actually has been laid out and has a content rect, else false.

getCustomSize

public int getCustomSize()

Returns the custom size for this FloatingActionButton.

Returns
int

size in pixels, or NO_CUSTOM_SIZE

getExpandedComponentIdHint

public int getExpandedComponentIdHint()

Returns the expanded component id hint.

getHideMotionSpec

public MotionSpec getHideMotionSpec()

Returns the motion spec for the hide animation.

getMeasuredContentRect

public void getMeasuredContentRect(Rect rect)

Return in rect the bounds of the actual floating action button content in view-local coordinates. This is defined as anything within any visible shadow.

getRippleColor

public int getRippleColor()

Returns the ripple color for this button.

Returns
int

the ARGB color used for the ripple

See also
setRippleColor

getRippleColorStateList

public ColorStateList getRippleColorStateList()

Returns the ripple color for this button.

Returns
ColorStateList

the color state list used for the ripple

See also
setRippleColor

getShapeAppearanceModel

public ShapeAppearanceModel getShapeAppearanceModel()

Returns the ShapeAppearanceModel for this FloatingActionButton.

getShowMotionSpec

public MotionSpec getShowMotionSpec()

Returns the motion spec for the show animation.

getSize

public int getSize()

Returns the chosen size for this button.

Returns
int

one of SIZE_NORMAL, SIZE_MINI or SIZE_AUTO

See also
setSize

getSupportBackgroundTintList

public ColorStateList getSupportBackgroundTintList()

Compat method to support TintableBackgroundView. Use getBackgroundTintList directly instead.

getSupportBackgroundTintMode

public Mode getSupportBackgroundTintMode()

Compat method to support TintableBackgroundView. Use getBackgroundTintMode directly instead.

getSupportImageTintList

public ColorStateList getSupportImageTintList()

getSupportImageTintMode

public Mode getSupportImageTintMode()

getUseCompatPadding

public boolean getUseCompatPadding()

Returns whether FloatingActionButton will add inner padding on platforms Lollipop and after.

ref com.google.android.material.R.styleable#FloatingActionButton_useCompatPadding

Returns
boolean

true if FloatingActionButton is adding inner padding on platforms Lollipop and after, to ensure consistent dimensions on all platforms.

getUserSetVisibility

public final int getUserSetVisibility()

hide

public void hide()

Hides the button.

This method will animate the button hide if the view has already been laid out.

hide

public void hide(FloatingActionButton.OnVisibilityChangedListener listener)

Hides the button.

This method will animate the button hide if the view has already been laid out.

Parameters
FloatingActionButton.OnVisibilityChangedListener listener

the listener to notify when this view is hidden

internalSetVisibility

public final void internalSetVisibility(int visibility, boolean fromUser)

isExpanded

public boolean isExpanded()

Returns whether this widget is expanded.

Implementations should call isExpanded.

isOrWillBeHidden

public boolean isOrWillBeHidden()

isOrWillBeShown

public boolean isOrWillBeShown()

jumpDrawablesToCurrentState

public void jumpDrawablesToCurrentState()

onTouchEvent

public boolean onTouchEvent(MotionEvent ev)

removeOnHideAnimationListener

public void removeOnHideAnimationListener(AnimatorListener listener)

removeOnShowAnimationListener

public void removeOnShowAnimationListener(AnimatorListener listener)

removeTransformationCallback

public void removeTransformationCallback(
    TransformationCallback<FloatingActionButton> listener
)

Remove the TransformationCallback from this view. It will no longer receive updates when this view is transformed.

setBackgroundColor

public void setBackgroundColor(int color)

setBackgroundDrawable

public void setBackgroundDrawable(Drawable background)

setBackgroundResource

public void setBackgroundResource(int resid)

setBackgroundTintList

public void setBackgroundTintList(ColorStateList tint)

Applies a tint to the background drawable. Does not modify the current tint mode, which is SRC_IN by default.

Parameters
ColorStateList tint

the tint to apply, may be null to clear tint

setBackgroundTintMode

public void setBackgroundTintMode(Mode tintMode)

Specifies the blending mode used to apply the tint specified by setBackgroundTintList} to the background drawable. The default mode is SRC_IN.

Parameters
Mode tintMode

the blending mode used to apply the tint, may be null to clear tint

setCompatElevation

public void setCompatElevation(float elevation)

Updates the backward compatible elevation of the FloatingActionButton.

ref com.google.android.material.R.styleable#FloatingActionButton_elevation

Parameters
float elevation

The backward compatible elevation in pixels.

setCompatElevationResource

public void setCompatElevationResource(int id)

Updates the backward compatible elevation of the FloatingActionButton.

ref com.google.android.material.R.styleable#FloatingActionButton_elevation

Parameters
int id

The resource id of the backward compatible elevation.

setCompatHoveredFocusedTranslationZ

public void setCompatHoveredFocusedTranslationZ(float translationZ)

Updates the backward compatible hovered/focused translationZ of the FloatingActionButton.

ref com.google.android.material.R.styleable#FloatingActionButton_hoveredFocusedTranslationZ

Parameters
float translationZ

The backward compatible hovered/focused translationZ in pixels.

setCompatHoveredFocusedTranslationZResource

public void setCompatHoveredFocusedTranslationZResource(int id)

Updates the backward compatible hovered/focused translationZ of the FloatingActionButton.

ref com.google.android.material.R.styleable#FloatingActionButton_hoveredFocusedTranslationZ

Parameters
int id

The resource id of the backward compatible hovered/focused translationZ.

setCompatPressedTranslationZ

public void setCompatPressedTranslationZ(float translationZ)

Updates the backward compatible pressed translationZ of the FloatingActionButton.

ref com.google.android.material.R.styleable#FloatingActionButton_pressedTranslationZ

Parameters
float translationZ

The backward compatible pressed translationZ in pixels.

setCompatPressedTranslationZResource

public void setCompatPressedTranslationZResource(int id)

Updates the backward compatible pressed translationZ of the FloatingActionButton.

ref com.google.android.material.R.styleable#FloatingActionButton_pressedTranslationZ

Parameters
int id

The resource id of the backward compatible pressed translationZ.

setCustomSize

public void setCustomSize(int size)

Sets the size of the button to be a custom value in pixels.

If you've set a custom size and would like to clear it, you can use the clearCustomSize method. If called, custom sizing will not be used and the size will be calculated based on the value set using setSize or the fabSize attribute.

ref com.google.android.material.R.styleable#FloatingActionButton_fabCustomSize

Parameters
int size

preferred size in pixels, or NO_CUSTOM_SIZE

setElevation

public void setElevation(float elevation)

setEnsureMinTouchTargetSize

public void setEnsureMinTouchTargetSize(boolean flag)

Sets whether this FloatingActionButton should expand its bounds (if needed) to meet the minimum touch target size.

ref com.google.android.material.R.styleable#FloatingActionButton_ensureMinTouchTargetSize

setExpanded

public boolean setExpanded(boolean expanded)

Sets the expanded state on this widget.

Implementations should call setExpanded.

Returns
boolean

true if the expanded state changed as a result of this call.

setExpandedComponentIdHint

public void setExpandedComponentIdHint(int expandedComponentIdHint)

Sets the expanded component id hint, which may be used by a Behavior to determine whether it should handle this widget's state change.

setHideMotionSpec

public void setHideMotionSpec(MotionSpec spec)

Updates the motion spec for the hide animation.

ref com.google.android.material.R.styleable#FloatingActionButton_hideMotionSpec

setHideMotionSpecResource

public void setHideMotionSpecResource(int id)

Updates the motion spec for the hide animation.

ref com.google.android.material.R.styleable#FloatingActionButton_hideMotionSpec

setImageDrawable

public void setImageDrawable(Drawable drawable)

setImageResource

public void setImageResource(int resId)

setMaxImageSize

public void setMaxImageSize(int imageSize)

Sets the max image size for this button.

ref com.google.android.material.R.styleable#FloatingActionButton_maxImageSize

Parameters
int imageSize

maximum icon image size

setRippleColor

public void setRippleColor(ColorStateList color)

Sets the ripple color for this button.

When running on devices with KitKat, we draw this color as a filled circle rather than a ripple.

ref com.google.android.material.R.styleable#FloatingActionButton_rippleColor

Parameters
ColorStateList color

color state list to use for the ripple

See also
getRippleColor

setRippleColor

public void setRippleColor(int color)

Sets the ripple color for this button.

When running on devices with KitKat, we draw this color as a filled circle rather than a ripple.

ref com.google.android.material.R.styleable#FloatingActionButton_rippleColor

Parameters
int color

ARGB color to use for the ripple

See also
getRippleColor

setScaleX

public void setScaleX(float scaleX)

setScaleY

public void setScaleY(float scaleY)

setShapeAppearanceModel

public void setShapeAppearanceModel(ShapeAppearanceModel shapeAppearance)

Sets the ShapeAppearanceModel for this FloatingActionButton.

setShowMotionSpec

public void setShowMotionSpec(MotionSpec spec)

Updates the motion spec for the show animation.

ref com.google.android.material.R.styleable#FloatingActionButton_showMotionSpec

setShowMotionSpecResource

public void setShowMotionSpecResource(int id)

Updates the motion spec for the show animation.

ref com.google.android.material.R.styleable#FloatingActionButton_showMotionSpec

setSize

public void setSize(int size)

Sets the size of the button.

The options relate to the options available on the material design specification. SIZE_NORMAL is larger than SIZE_MINI. SIZE_AUTO will choose an appropriate size based on the screen size.

Calling this method will turn off custom sizing (see setCustomSize) if it was previously on.

ref com.google.android.material.R.styleable#FloatingActionButton_fabSize

Parameters
int size

one of SIZE_NORMAL, SIZE_MINI or SIZE_AUTO

setSupportBackgroundTintList

public void setSupportBackgroundTintList(ColorStateList tint)

Compat method to support TintableBackgroundView. Use setBackgroundTintList directly instead.

setSupportBackgroundTintMode

public void setSupportBackgroundTintMode(Mode tintMode)

Compat method to support TintableBackgroundView. Use setBackgroundTintMode directly instead.

setSupportImageTintList

public void setSupportImageTintList(ColorStateList tint)

setSupportImageTintMode

public void setSupportImageTintMode(Mode tintMode)

setTranslationX

public void setTranslationX(float translationX)

setTranslationY

public void setTranslationY(float translationY)

setTranslationZ

public void setTranslationZ(float translationZ)

setUseCompatPadding

public void setUseCompatPadding(boolean useCompatPadding)

Set whether FloatingActionButton should add inner padding on platforms Lollipop and after, to ensure consistent dimensions on all platforms.

ref com.google.android.material.R.styleable#FloatingActionButton_useCompatPadding

Parameters
boolean useCompatPadding

true if FloatingActionButton is adding inner padding on platforms Lollipop and after, to ensure consistent dimensions on all platforms.

setVisibility

public void setVisibility(int visibility)

shouldEnsureMinTouchTargetSize

public boolean shouldEnsureMinTouchTargetSize()

Returns whether this fab will expand its bounds (if needed) to meet the minimum touch target size.

ref com.google.android.material.R.styleable#FloatingActionButton_ensureMinTouchTargetSize

show

public void show()

Shows the button.

This method will animate the button show if the view has already been laid out.

show

public void show(FloatingActionButton.OnVisibilityChangedListener listener)

Shows the button.

This method will animate the button show if the view has already been laid out.

Parameters
FloatingActionButton.OnVisibilityChangedListener listener

the listener to notify when this view is shown

Protected methods

drawableStateChanged

protected void drawableStateChanged()

onAttachedToWindow

protected void onAttachedToWindow()

onDetachedFromWindow

protected void onDetachedFromWindow()

onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

onRestoreInstanceState

protected void onRestoreInstanceState(Parcelable state)

onSaveInstanceState

protected Parcelable onSaveInstanceState()