Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

FloatingActionButton

public class FloatingActionButton
extends ImageButton implements ExpandableTransformationWidget

java.lang.Object
   ↳ ImageButton
     ↳ com.google.android.material.floatingactionbutton.FloatingActionButton


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 ERROR(/ImageView), you can control the icon which is displayed via setImageDrawable(Drawable).

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

Summary

Nested classes

class FloatingActionButton.BaseBehavior<T extends FloatingActionButton>

Behavior designed for use with FloatingActionButton instances. 

class FloatingActionButton.Behavior

Behavior designed for use with FloatingActionButton instances. 

class FloatingActionButton.OnVisibilityChangedListener

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

Constants

int NO_CUSTOM_SIZE

Indicates that the FloatingActionButton should not have a custom size.

int SIZE_AUTO

Size which will change based on the window size.

int SIZE_MINI

The mini sized button.

int SIZE_NORMAL

The normal sized button.

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)
ColorStateList getBackgroundTintList()

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

PorterDuff.Mode getBackgroundTintMode()

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

float getCompatElevation()

Returns the backward compatible elevation of the FloatingActionButton.

float getCompatHoveredFocusedTranslationZ()

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

float getCompatPressedTranslationZ()

Returns the backward compatible pressed translationZ of the FloatingActionButton.

Drawable getContentBackground()

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

boolean getContentRect(Rect rect)

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

int getCustomSize()

Returns the custom size for this FloatingActionButton.

int getExpandedComponentIdHint()

Returns the expanded component id hint.

MotionSpec getHideMotionSpec()

Returns the motion spec for the hide animation.

int getRippleColor()

This method is deprecated. Use getRippleColorStateList() instead.

ColorStateList getRippleColorStateList()

Returns the ripple color for this button.

MotionSpec getShowMotionSpec()

Returns the motion spec for the show animation.

int getSize()

Returns the chosen size for this button.

ColorStateList getSupportBackgroundTintList()

Compat method to support ERROR(/TintableBackgroundView).

Mode getSupportBackgroundTintMode()

Compat method to support ERROR(/TintableBackgroundView).

ColorStateList getSupportImageTintList()
Mode getSupportImageTintMode()
boolean getUseCompatPadding()

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

final int getUserSetVisibility()
void hide()

Hides the button.

void hide(FloatingActionButton.OnVisibilityChangedListener listener)

Hides the button.

final void internalSetVisibility(int visibility, boolean fromUser)
boolean isExpanded()

Returns whether this widget is expanded.

boolean isOrWillBeHidden()
boolean isOrWillBeShown()
void jumpDrawablesToCurrentState()
boolean onTouchEvent(MotionEvent ev)
void removeOnHideAnimationListener(AnimatorListener listener)
void removeOnShowAnimationListener(AnimatorListener listener)
void setBackgroundColor(int color)
void setBackgroundDrawable(Drawable background)
void setBackgroundResource(int resid)
void setBackgroundTintList(ColorStateList tint)

Applies a tint to the background drawable.

void setBackgroundTintMode(PorterDuff.Mode tintMode)

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

void setCompatElevation(float elevation)

Updates the backward compatible elevation of the FloatingActionButton.

void setCompatElevationResource(int id)

Updates the backward compatible elevation of the FloatingActionButton.

void setCompatHoveredFocusedTranslationZ(float translationZ)

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

void setCompatHoveredFocusedTranslationZResource(int id)

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

void setCompatPressedTranslationZ(float translationZ)

Updates the backward compatible pressed translationZ of the FloatingActionButton.

void setCompatPressedTranslationZResource(int id)

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.

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 setHideMotionSpec(MotionSpec spec)

Updates the motion spec for the hide animation.

void setHideMotionSpecResource(int id)

Updates the motion spec for the hide animation.

void setImageDrawable(Drawable drawable)
void setImageResource(int resId)
void setRippleColor(ColorStateList color)

Sets the ripple color for this button.

void setRippleColor(int color)

Sets the ripple color for this button.

void setShowMotionSpec(MotionSpec spec)

Updates the motion spec for the show animation.

void setShowMotionSpecResource(int id)

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 ERROR(/TintableBackgroundView).

void setSupportBackgroundTintMode(Mode tintMode)

Compat method to support ERROR(/TintableBackgroundView).

void setSupportImageTintList(ColorStateList tint)
void setSupportImageTintMode(Mode tintMode)
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)
void show()

Shows the button.

void show(FloatingActionButton.OnVisibilityChangedListener listener)

Shows the button.

Protected methods

void drawableStateChanged()
void onAttachedToWindow()
void onDetachedFromWindow()
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void onRestoreInstanceState(Parcelable state)
Parcelable onSaveInstanceState()

Inherited methods

From class java.lang.Object
From interface com.google.android.material.expandable.ExpandableTransformationWidget
From interface com.google.android.material.expandable.ExpandableWidget

Constants

NO_CUSTOM_SIZE

int NO_CUSTOM_SIZE

Indicates that the FloatingActionButton should not have a custom size.

Constant Value: 0 (0x00000000)

SIZE_AUTO

int SIZE_AUTO

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

See also:

Constant Value: -1 (0xffffffff)

SIZE_MINI

int SIZE_MINI

The mini sized button. Will always been smaller than SIZE_NORMAL.

See also:

Constant Value: 1 (0x00000001)

SIZE_NORMAL

int SIZE_NORMAL

The normal sized button. Will always been larger than SIZE_MINI.

See also:

Constant Value: 0 (0x00000000)

Public constructors

FloatingActionButton

FloatingActionButton (Context context)

Parameters
context Context

FloatingActionButton

FloatingActionButton (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

FloatingActionButton

FloatingActionButton (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

addOnHideAnimationListener

void addOnHideAnimationListener (AnimatorListener listener)

Parameters
listener AnimatorListener

addOnShowAnimationListener

void addOnShowAnimationListener (AnimatorListener listener)

Parameters
listener AnimatorListener

getBackgroundTintList

ColorStateList getBackgroundTintList ()

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

Returns
ColorStateList the tint applied to the background drawable

getBackgroundTintMode

PorterDuff.Mode getBackgroundTintMode ()

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

Returns
PorterDuff.Mode the blending mode used to apply the tint to the background drawable

getCompatElevation

float getCompatElevation ()

Returns the backward compatible elevation of the FloatingActionButton.

Returns
float the backward compatible elevation in pixels.

getCompatHoveredFocusedTranslationZ

float getCompatHoveredFocusedTranslationZ ()

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

Returns
float the backward compatible hovered/focused translationZ in pixels.

getCompatPressedTranslationZ

float getCompatPressedTranslationZ ()

Returns the backward compatible pressed translationZ of the FloatingActionButton.

Returns
float the backward compatible pressed translationZ in pixels.

getContentBackground

Drawable getContentBackground ()

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

Returns
Drawable

getContentRect

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.

Parameters
rect Rect

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

getCustomSize

int getCustomSize ()

Returns the custom size for this FloatingActionButton.

Returns
int size in pixels, or NO_CUSTOM_SIZE

getExpandedComponentIdHint

int getExpandedComponentIdHint ()

Returns the expanded component id hint.

Returns
int

getHideMotionSpec

MotionSpec getHideMotionSpec ()

Returns the motion spec for the hide animation.

Returns
MotionSpec

getRippleColor

int getRippleColor ()

This method is deprecated.
Use getRippleColorStateList() instead.

Returns the ripple color for this button.

Returns
int the ARGB color used for the ripple

getRippleColorStateList

ColorStateList getRippleColorStateList ()

Returns the ripple color for this button.

Returns
ColorStateList the color state list used for the ripple

getShowMotionSpec

MotionSpec getShowMotionSpec ()

Returns the motion spec for the show animation.

Returns
MotionSpec

getSize

int getSize ()

Returns the chosen size for this button.

Returns
int one of SIZE_NORMAL, SIZE_MINI or SIZE_AUTO

See also:

getSupportBackgroundTintList

ColorStateList getSupportBackgroundTintList ()

Compat method to support ERROR(/TintableBackgroundView). Use getBackgroundTintList() directly instead.

Returns
ColorStateList

getSupportBackgroundTintMode

Mode getSupportBackgroundTintMode ()

Compat method to support ERROR(/TintableBackgroundView). Use getBackgroundTintMode() directly instead.

Returns
Mode

getSupportImageTintList

ColorStateList getSupportImageTintList ()

Returns
ColorStateList

getSupportImageTintMode

Mode getSupportImageTintMode ()

Returns
Mode

getUseCompatPadding

boolean getUseCompatPadding ()

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

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

getUserSetVisibility

int getUserSetVisibility ()

Returns
int

hide

void hide ()

Hides the button.

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

hide

void hide (FloatingActionButton.OnVisibilityChangedListener listener)

Hides the button.

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

Parameters
listener FloatingActionButton.OnVisibilityChangedListener: the listener to notify when this view is hidden

internalSetVisibility

void internalSetVisibility (int visibility, 
                boolean fromUser)

Parameters
visibility int

fromUser boolean

isExpanded

boolean isExpanded ()

Returns whether this widget is expanded.

Implementations should call isExpanded().

Returns
boolean

isOrWillBeHidden

boolean isOrWillBeHidden ()

Returns
boolean

isOrWillBeShown

boolean isOrWillBeShown ()

Returns
boolean

jumpDrawablesToCurrentState

void jumpDrawablesToCurrentState ()

onTouchEvent

boolean onTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

removeOnHideAnimationListener

void removeOnHideAnimationListener (AnimatorListener listener)

Parameters
listener AnimatorListener

removeOnShowAnimationListener

void removeOnShowAnimationListener (AnimatorListener listener)

Parameters
listener AnimatorListener

setBackgroundColor

void setBackgroundColor (int color)

Parameters
color int

setBackgroundDrawable

void setBackgroundDrawable (Drawable background)

Parameters
background Drawable

setBackgroundResource

void setBackgroundResource (int resid)

Parameters
resid int

setBackgroundTintList

void setBackgroundTintList (ColorStateList tint)

Applies a tint to the background drawable. Does not modify the current tint mode, which is ERROR(/PorterDuff.Mode#SRC_IN) by default.

Parameters
tint ColorStateList: the tint to apply, may be null to clear tint

setBackgroundTintMode

void setBackgroundTintMode (PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setBackgroundTintList(ColorStateList)} to the background drawable. The default mode is ERROR(/PorterDuff.Mode#SRC_IN).

Parameters
tintMode PorterDuff.Mode: the blending mode used to apply the tint, may be null to clear tint

setCompatElevation

void setCompatElevation (float elevation)

Updates the backward compatible elevation of the FloatingActionButton.

Parameters
elevation float: The backward compatible elevation in pixels.

setCompatElevationResource

void setCompatElevationResource (int id)

Updates the backward compatible elevation of the FloatingActionButton.

Parameters
id int: The resource id of the backward compatible elevation.

setCompatHoveredFocusedTranslationZ

void setCompatHoveredFocusedTranslationZ (float translationZ)

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

Parameters
translationZ float: The backward compatible hovered/focused translationZ in pixels.

setCompatHoveredFocusedTranslationZResource

void setCompatHoveredFocusedTranslationZResource (int id)

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

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

setCompatPressedTranslationZ

void setCompatPressedTranslationZ (float translationZ)

Updates the backward compatible pressed translationZ of the FloatingActionButton.

Parameters
translationZ float: The backward compatible pressed translationZ in pixels.

setCompatPressedTranslationZResource

void setCompatPressedTranslationZResource (int id)

Updates the backward compatible pressed translationZ of the FloatingActionButton.

Parameters
id int: The resource id of the backward compatible pressed translationZ.

setCustomSize

void setCustomSize (int size)

Sets the size of the button to be a custom value in pixels. If set to NO_CUSTOM_SIZE, custom sizing will not be used and the size will be calculated based on the value set using setSize(int) or the fabSize attribute.

Parameters
size int: preferred size in pixels, or NO_CUSTOM_SIZE

setExpanded

boolean setExpanded (boolean expanded)

Sets the expanded state on this widget.

Implementations should call setExpanded(boolean).

Parameters
expanded boolean

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

setExpandedComponentIdHint

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.

Parameters
expandedComponentIdHint int

setHideMotionSpec

void setHideMotionSpec (MotionSpec spec)

Updates the motion spec for the hide animation.

Parameters
spec MotionSpec

setHideMotionSpecResource

void setHideMotionSpecResource (int id)

Updates the motion spec for the hide animation.

Parameters
id int

setImageDrawable

void setImageDrawable (Drawable drawable)

Parameters
drawable Drawable

setImageResource

void setImageResource (int resId)

Parameters
resId int

setRippleColor

void setRippleColor (ColorStateList color)

Sets the ripple color for this button.

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

Parameters
color ColorStateList: color state list to use for the ripple

See also:

setRippleColor

void setRippleColor (int color)

Sets the ripple color for this button.

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

Parameters
color int: ARGB color to use for the ripple

See also:

setShowMotionSpec

void setShowMotionSpec (MotionSpec spec)

Updates the motion spec for the show animation.

Parameters
spec MotionSpec

setShowMotionSpecResource

void setShowMotionSpecResource (int id)

Updates the motion spec for the show animation.

Parameters
id int

setSize

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(int)) if it was previously on.

Parameters
size int: one of SIZE_NORMAL, SIZE_MINI or SIZE_AUTO

setSupportBackgroundTintList

void setSupportBackgroundTintList (ColorStateList tint)

Compat method to support ERROR(/TintableBackgroundView). Use setBackgroundTintList(ColorStateList) directly instead.

Parameters
tint ColorStateList

setSupportBackgroundTintMode

void setSupportBackgroundTintMode (Mode tintMode)

Compat method to support ERROR(/TintableBackgroundView). Use setBackgroundTintMode(Mode) directly instead.

Parameters
tintMode Mode

setSupportImageTintList

void setSupportImageTintList (ColorStateList tint)

Parameters
tint ColorStateList

setSupportImageTintMode

void setSupportImageTintMode (Mode tintMode)

Parameters
tintMode Mode

setUseCompatPadding

void setUseCompatPadding (boolean useCompatPadding)

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

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

setVisibility

void setVisibility (int visibility)

Parameters
visibility int

show

void show ()

Shows the button.

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

show

void show (FloatingActionButton.OnVisibilityChangedListener listener)

Shows the button.

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

Parameters
listener FloatingActionButton.OnVisibilityChangedListener: the listener to notify when this view is shown

Protected methods

drawableStateChanged

void drawableStateChanged ()

onAttachedToWindow

void onAttachedToWindow ()

onDetachedFromWindow

void onDetachedFromWindow ()

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onRestoreInstanceState

void onRestoreInstanceState (Parcelable state)

Parameters
state Parcelable

onSaveInstanceState

Parcelable onSaveInstanceState ()

Returns
Parcelable