Most visited

Recently visited

FloatingActionButton

public class FloatingActionButton
extends ImageButton

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

Behavior designed for use with FloatingActionButton instances. 

class FloatingActionButton.OnVisibilityChangedListener

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

XML attributes

android.support.design:elevation Elevation value for the FAB

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name". 

android.support.design:fabSize Size for the FAB. 
android.support.design:rippleColor Ripple color for the FAB. 
android.support.design:useCompatPadding Enable compat padding. 

Inherited XML attributes

From class android.widget.ImageView
From class android.view.View

Constants

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.

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

ColorStateList getBackgroundTintList()

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

PorterDuff.Mode getBackgroundTintMode()

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

float getCompatElevation()

Returns the backward compatible elevation 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 getSize()

Returns the chosen size for this button.

boolean getUseCompatPadding()

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

void hide()

Hides the button.

void hide(FloatingActionButton.OnVisibilityChangedListener listener)

Hides the button.

void jumpDrawablesToCurrentState()

Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.

boolean onTouchEvent(MotionEvent ev)

Implement this method to handle touch screen motion events.

void setBackgroundColor(int color)

Sets the background color for this view.

void setBackgroundDrawable(Drawable background)

This method is deprecated. use setBackground(Drawable) instead

void setBackgroundResource(int resid)

Set the background to a given resource.

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 setImageResource(int resId)

Sets a drawable as the content of this ImageView.

void setRippleColor(int color)

Set the ripple color for this FloatingActionButton.

void setSize(int size)

Sets the size of the button.

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)

Set the enabled state of this view.

void show(FloatingActionButton.OnVisibilityChangedListener listener)

Shows the button.

void show()

Shows the button.

Protected methods

void drawableStateChanged()

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

void onAttachedToWindow()

This is called when the view is attached to a window.

void onDetachedFromWindow()

This is called when the view is detached from a window.

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

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

XML attributes

android.support.design:elevation

Elevation value for the FAB

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

May be an integer value, such as "100".

May be a boolean value, such as "true" or "false".

May be a color value, in the form of "#rgb", "#argb", "#rrggbb#aarrggbb".

May be a floating point value, such as "1.2".

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

May be a fractional value, which is a floating point number appended with either % or %p, such as "14.5%". The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to some parent container.

Related methods:

android.support.design:fabSize

Size for the FAB.

Must be one of the following constant values.

ConstantValueDescription
autoffffffffA size which will change based on the window size.
mini1The mini sized button.
normal0The normal sized button.

Related methods:

android.support.design:rippleColor

Ripple color for the FAB.

May be a color value, in the form of "#rgb", "#argb", "#rrggbb#aarrggbb".

Related methods:

android.support.design:useCompatPadding

Enable compat padding.

May be a boolean value, such as "true" or "false".

Related methods:

Constants

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

getBackgroundTintList

ColorStateList getBackgroundTintList ()

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

Returns
ColorStateList the tint applied to the background drawable

See also:

getBackgroundTintMode

PorterDuff.Mode getBackgroundTintMode ()

Return 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

See also:

getCompatElevation

float getCompatElevation ()

Returns the backward compatible elevation of the FloatingActionButton.

Related XML Attributes:

Returns
float the backward compatible elevation in pixels.

See also:

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.

getSize

int getSize ()

Returns the chosen size for this button.

Returns
int one of SIZE_NORMAL, SIZE_MINI or SIZE_AUTO

See also:

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.

See also:

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

jumpDrawablesToCurrentState

void jumpDrawablesToCurrentState ()

Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.

Also calls jumpToCurrentState() if there is a StateListAnimator attached to this view.

onTouchEvent

boolean onTouchEvent (MotionEvent ev)

Implement this method to handle touch screen motion events.

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

Parameters
ev MotionEvent: The motion event.
Returns
boolean True if the event was handled, false otherwise.

setBackgroundColor

void setBackgroundColor (int color)

Sets the background color for this view.

Parameters
color int: the color of the background

setBackgroundDrawable

void setBackgroundDrawable (Drawable background)

This method is deprecated.
use setBackground(Drawable) instead

Parameters
background Drawable

setBackgroundResource

void setBackgroundResource (int resid)

Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.

Parameters
resid int: The identifier of the resource.

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.

See also:

setImageResource

void setImageResource (int resId)

Sets a drawable as the content of this ImageView.

This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.

Parameters
resId int: the resource identifier of the drawable

setRippleColor

void setRippleColor (int color)

Set the ripple color for this FloatingActionButton.

When running on devices with KitKat or below, we draw a fill rather than a ripple.

Related XML Attributes:

Parameters
color int: ARGB color to use for the ripple.

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.

Related XML Attributes:

Parameters
size int: one of SIZE_NORMAL, SIZE_MINI or SIZE_AUTO

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.

See also:

setVisibility

void setVisibility (int visibility)

Set the enabled state of this view.

Parameters
visibility int: One of VISIBLE, INVISIBLE, or GONE.

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

show

void show ()

Shows the button.

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

Protected methods

drawableStateChanged

void drawableStateChanged ()

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

If the View has a StateListAnimator, it will also be called to run necessary state change animations.

Be sure to call through to the superclass when overriding this function.

onAttachedToWindow

void onAttachedToWindow ()

This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called before onDraw(android.graphics.Canvas), however it may be called any time before the first onDraw -- including before or after onMeasure(int, int).

onDetachedFromWindow

void onDetachedFromWindow ()

This is called when the view is detached from a window. At this point it no longer has a surface for drawing.

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

Hooray!