FloatingActionButton

public class FloatingActionButton
extends ImageButton implements TintableBackgroundView, TintableImageSourceView, ExpandableTransformationWidget

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.ImageView
       ↳ android.widget.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 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. 

XML attributes

FloatingActionButton_elevation  
FloatingActionButton_fabSize  
FloatingActionButton_hideMotionSpec  
FloatingActionButton_hoveredFocusedTranslationZ  
FloatingActionButton_pressedTranslationZ  
FloatingActionButton_rippleColor  
FloatingActionButton_showMotionSpec  
FloatingActionButton_useCompatPadding  

Constants

int NO_CUSTOM_SIZE

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(int) or the fabSize attribute.

int SIZE_AUTO

Size which will change based on the window size.

int SIZE_MINI

The mini sized button, 40dp.

int SIZE_NORMAL

The normal sized button, 56dp.

Inherited constants

From class android.view.View

Inherited fields

From class android.view.View

Public constructors

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

Public methods

void addOnHideAnimationListener(Animator.AnimatorListener listener)
void addOnShowAnimationListener(Animator.AnimatorListener listener)
void clearCustomSize()

Clears the custom size for this FloatingActionButton.

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)

This method is deprecated. prefer getMeasuredContentRect(Rect) instead, so you don't need to handle the case where the view isn't laid out.

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.

void getMeasuredContentRect(Rect rect)

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

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 TintableBackgroundView.

PorterDuff.Mode getSupportBackgroundTintMode()

Compat method to support TintableBackgroundView.

ColorStateList getSupportImageTintList()
PorterDuff.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(Animator.AnimatorListener listener)
void removeOnShowAnimationListener(Animator.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 TintableBackgroundView.

void setSupportBackgroundTintMode(PorterDuff.Mode tintMode)

Compat method to support TintableBackgroundView.

void setSupportImageTintList(ColorStateList tint)
void setSupportImageTintMode(PorterDuff.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 android.widget.ImageButton
From class android.widget.ImageView
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource
From interface android.support.v4.view.TintableBackgroundView
From interface android.support.v4.widget.TintableImageSourceView
From interface com.google.android.material.expandable.ExpandableTransformationWidget
From interface com.google.android.material.expandable.ExpandableWidget

XML attributes

FloatingActionButton_elevation

Related methods:

FloatingActionButton_fabSize

Related methods:

FloatingActionButton_hideMotionSpec

Related methods:

FloatingActionButton_hoveredFocusedTranslationZ

Related methods:

FloatingActionButton_pressedTranslationZ

Related methods:

FloatingActionButton_rippleColor

Related methods:

FloatingActionButton_showMotionSpec

Related methods:

FloatingActionButton_useCompatPadding

Related methods:

Constants

NO_CUSTOM_SIZE

int NO_CUSTOM_SIZE

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(int) or the fabSize attribute. Instead of using this constant directly, you can call the clearCustomSize() method.

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 mini sized button (SIZE_MINI), and for larger sized windows it will select a normal sized button (SIZE_NORMAL).

See also:

Constant Value: -1 (0xffffffff)

SIZE_MINI

int SIZE_MINI

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

See also:

Constant Value: 1 (0x00000001)

SIZE_NORMAL

int SIZE_NORMAL

The normal sized button, 56dp. Will always be 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 (Animator.AnimatorListener listener)

Parameters
listener Animator.AnimatorListener

addOnShowAnimationListener

void addOnShowAnimationListener (Animator.AnimatorListener listener)

Parameters
listener Animator.AnimatorListener

clearCustomSize

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(int) or the fabSize attribute

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.

Related XML Attributes:

Returns
float the backward compatible elevation in pixels.

getCompatHoveredFocusedTranslationZ

float getCompatHoveredFocusedTranslationZ ()

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

Related XML Attributes:

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

getCompatPressedTranslationZ

float getCompatPressedTranslationZ ()

Returns the backward compatible pressed translationZ of the FloatingActionButton.

Related XML Attributes:

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)

This method is deprecated.
prefer getMeasuredContentRect(Rect) instead, so you don't need to handle the case where the view isn't laid out.

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

getMeasuredContentRect

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.

Parameters
rect Rect

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 TintableBackgroundView. Use getBackgroundTintList() directly instead.

Returns
ColorStateList

getSupportBackgroundTintMode

PorterDuff.Mode getSupportBackgroundTintMode ()

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

Returns
PorterDuff.Mode

getSupportImageTintList

ColorStateList getSupportImageTintList ()

Returns
ColorStateList

getSupportImageTintMode

PorterDuff.Mode getSupportImageTintMode ()

Returns
PorterDuff.Mode

getUseCompatPadding

boolean getUseCompatPadding ()

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

Related XML Attributes:

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 (Animator.AnimatorListener listener)

Parameters
listener Animator.AnimatorListener

removeOnShowAnimationListener

void removeOnShowAnimationListener (Animator.AnimatorListener listener)

Parameters
listener Animator.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 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 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.

Related XML Attributes:

Parameters
elevation float: The backward compatible elevation in pixels.

setCompatElevationResource

void setCompatElevationResource (int id)

Updates the backward compatible elevation of the FloatingActionButton.

Related XML Attributes:

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.

Related XML Attributes:

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.

Related XML Attributes:

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.

Related XML Attributes:

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

setCompatPressedTranslationZResource

void setCompatPressedTranslationZResource (int id)

Updates the backward compatible pressed translationZ of the FloatingActionButton.

Related XML Attributes:

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 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(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.

Related XML Attributes:

Parameters
spec MotionSpec

setHideMotionSpecResource

void setHideMotionSpecResource (int id)

Updates the motion spec for the hide animation.

Related XML Attributes:

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.

Related XML Attributes:

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.

Related XML Attributes:

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.

Related XML Attributes:

Parameters
spec MotionSpec

setShowMotionSpecResource

void setShowMotionSpecResource (int id)

Updates the motion spec for the show animation.

Related XML Attributes:

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.

Related XML Attributes:

Parameters
size int: one of SIZE_NORMAL, SIZE_MINI or SIZE_AUTO

setSupportBackgroundTintList

void setSupportBackgroundTintList (ColorStateList tint)

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

Parameters
tint ColorStateList

setSupportBackgroundTintMode

void setSupportBackgroundTintMode (PorterDuff.Mode tintMode)

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

Parameters
tintMode PorterDuff.Mode

setSupportImageTintList

void setSupportImageTintList (ColorStateList tint)

Parameters
tint ColorStateList

setSupportImageTintMode

void setSupportImageTintMode (PorterDuff.Mode tintMode)

Parameters
tintMode PorterDuff.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.

Related XML Attributes:

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