# Floating action buttons A [floating action button (FAB)](https://material.io/components/buttons-floating-action-button) represents the primary action of a screen. !["A FAB on a screen"](assets/fabs/fab_hero.png) ## Contents * [Using FABs](#using-fabs) * [Regular FABs](#regular-fabs) * [Mini FABs](#mini-fabs) * [Extended FABs](#extended-fabs) * [Theming FABs](#theming-fabs) ## Using FABs A FAB performs the primary, or most common, action on a screen. It appears in front of all screen content, typically as a circular shape with an icon in its center. Before you can use Material FABs, you need to add a dependency to the Material Components for Android library. For more information, go to the [Getting started](https://github.com/material-components/material-components-android/tree/master/docs/getting-started.md) page. Note: If the `FloatingActionButton` is a child of a `CoordinatorLayout`, you get certain behaviors for free. It will automatically shift so that any displayed `Snackbar`s do not cover it, and will automatially hide when covered by an `AppBarLayout` or `BottomSheetBehavior`. ### Making FABs accessible You should set a content description on a FAB via the `android:contentDescription` attribute or `setContentDescription` method so that screen readers like TalkBack are able to announce their purpose or action. Text rendered in Extended FABs is automatically provided to accessibility services, so additional content labels are usually unnecessary. ### Visibility Use the `show` and `hide` methods to animate the visibility of a `FloatingActionButton` or an `ExtendedFloatingActionButton`. The show animation grows the widget and fades it in, while the hide animation shrinks the widget and fades it out. ```kt // To show: fab.show() // To hide: fab.hide() ``` ### Extending and Shrinking Use the `extend` and `shrink` methods to animate showing and hiding the text of an `ExtendedFloatingActionButton`. The extend animation extends the FAB to show the text and the icon. The shrink animation shrinks the FAB to show just the icon. ```kt // To extend: extendedFab.extend() // To shrink: extendedFab.shrink() ``` ### Sizing FABs The `FloatingActionButton` can be sized either by using the discrete sizing modes or a custom size. There are three `app:fabSize` modes: * `normal` - the normal sized button, 56dp. * `mini` - the mini sized button, 40dp. * `auto` (default) - the button size will change based on the window size. For small sized windows (largest screen dimension < 470dp) this will select a mini sized button, and for larger sized windows it will select a normal sized button. Or, you can set a custom size via the `app:fabCustomSize` attribute. If set, `app:fabSize` will be ignored, unless the custom size is cleared via the `clearCustomSize` method. ## Types There are three types of FABS: 1\. [Regular FABs](#regular-fabs), 2\. [Mini FABs](#mini-fabs), 3\. [Extended FABs](#extended-fabs) ![Regular FAB example](assets/fabs/FAB_types.png) ## FABs Floating action buttons come in a default and mini size. API and source code: * `FloatingActionButton` * [Class description](https://developer.android.com/reference/com/google/android/material/floatingactionbutton/FloatingActionButton) * [Class source](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/FloatingActionButton.java) ### Regular FABs Regular FABs are FABs that are not expanded and are a regular size. #### Regular FAB example The following example shows a regular FAB with a plus icon. ![Regular FAB example with a plus icon](assets/fabs/fab_regular.png) In the layout: ```xml ``` In code: ```kt fab.setOnClickListener { // Respond to FAB click } ``` #### Anatomy ![Regular FAB anatomy diagram](assets/fabs/FAB_anatomy-long.png) A regular FAB has a container and an icon. 1. Container 1. Icon ### Mini FABs A mini FAB should be used on smaller screens. Mini FABs can also be used to create visual continuity with other screen elements. ### Mini FAB example The following example shows a mini FAB with a plus icon. ![Mini FAB example with a plus icon](assets/fabs/fab_mini.png) In the layout: ```xml ``` In code: ```kt fab.setOnClickListener { // Respond to FAB click } ``` #### Anatomy ![Mini FAB anatomy diagram](assets/fabs/miniFAB_anatomy-long.png) A mini FAB has a container and an icon. 1. Container 1. Icon ### Regular and mini FAB key properties #### Container attributes   | Attribute | Related method(s) | Default value ----------------------------- | ---------------------------------------------- | ------------------------------------------------------------------------------------- | ------------- **Color** | `app:backgroundTint` | `setBackgroundTintList`
`getBackgroundTintList` | `?attr/colorSecondary` (see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/color/mtrl_fab_bg_color_selector.xml)) **Stroke** | `app:borderWidth` | N/A | `0.5dp` **Size** | `app:fabSize`
`app:fabCustomSize` | `setSize`
`setCustomSize`
`clearCustomSize`
`getSize`
`getCustomSize` | `auto` **Shape** | `shapeAppearance`
`shapeAppearanceOverlay` | `setShapeAppearanceModel`
`getShapeAppearanceModel` | `?attr/shapeAppearanceSmallComponent`
**Elevation** | `app:elevation` | `setElevation`
`getCompatElevation` | `6dp` **Hovered/Focused elevation** | `app:hoveredFocusedTranslationZ` | `setCompatHoveredFocusedTranslationZ`
`getCompatHoveredFocusedTranslationZ` | `2dp` **Pressed elevation** | `app:pressedTranslationZ` | `setCompatPressedTranslationZ`
`getCompatPressedTranslationZ` | `6dp` **Ripple** | `app:rippleColor` | `setRippleColor`
`getRippleColor`
`getRippleColorStateList` | variations of `?attr/colorOnSecondary`, see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/color/mtrl_fab_ripple_color.xml) **Motion** | `app:showMotionSpec`
`app:hideMotionSpec` | `set*MotionSpec`
`set*MotionSpecResource`
`get*MotionSpec` | see [animators](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/animator) #### Icon attributes   | Attribute | Related method(s) | Default value --------- | --------------- | ----------------------------------------------------------- | ------------- **Icon** | `app:srcCompat` | `setImageDrawable`
`setImageResource`
`getDrawable` | `null` **Color** | `app:tint` | `setImageTintList`
`getImageTintList` | `?attr/colorOnSecondary` (see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/color/mtrl_fab_icon_text_color_selector.xml)) #### Styles   | Style ----------------- | ------------------------------------------------ **Default style** | `Widget.MaterialComponents.FloatingActionButton` Default style theme attribute: `?attr/floatingActionButtonStyle` See the full list of [styles](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/values/styles.xml) and [attrs](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/values/attrs.xml). ## Extended FABs The extended FAB is wider, and it includes a text label. Note: `ExtendedFloatingActionButton` is a child class of [`MaterialButton`](Button.md), rather than `FloatingActionButton`. This means that several attributes which are applicable to `FloatingActionButton` have different naming in `ExtendedFloatingActionButton`. For example, `FloatingActionButton` uses `app:srcCompat` to set the icon drawable, whereas `ExtendedFloatingActionButton` uses `app:icon`. See the [attributes tables](#anatomy-and-key-properties) below for more details. API and source code: * `ExtendedFloatingActionButton` * [Class description](https://developer.android.com/reference/com/google/android/material/floatingactionbutton/ExtendedFloatingActionButton) * [Class source](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/ExtendedFloatingActionButton.java) ### Extended FAB example The following example shows an extended FAB with a plus icon. ![Extended FAB example with a plus icon and the word "extended"](assets/fabs/fab_extended.png) In the layout: ```xml ``` In code: ```kt extendedFab.setOnClickListener { // Respond to Extended FAB click } ``` ### Anatomy and key properties ![Extended FAB anatomy diagram](assets/fabs/extended-FAB_anatomy-long.png) An extended FAB has a text label, a transparent container and an optional icon. 1. Container 1. Icon (Optional) 1. Text label #### Container attributes   | Attribute | Related method(s) | Default value ----------------------------- | ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------ | ------------- **Color** | `app:backgroundTint` | `setBackgroundTintList`
`getBackgroundTintList` | `?attr/colorSecondary` (see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/color/mtrl_fab_bg_color_selector.xml)) **Stroke color** | `app:strokeColor` | `setStrokeColor`
`getStrokeColor` | `null` **Stroke width** | `app:strokeWidth` | `setStrokeWidth`
`getStrokeWidth` | `0dp` **Shape** | `app:shapeAppearance`
`app:shapeAppearanceOverlay` | `setShapeAppearanceModel`
`getShapeAppearanceModel` | `?attr/shapeAppearanceSmallComponent` **Elevation** | `app:elevation` | `setElevation`
`getElevation` | `6dp` **Hovered/Focused elevation** | `app:hoveredFocusedTranslationZ` | N/A | `2dp` **Pressed elevation** | `app:pressedTranslationZ` | N/A | `6dp` **Ripple** | `app:rippleColor` | | variations of `?attr/colorOnSecondary`, see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/color/mtrl_fab_ripple_color.xml) **Motion** | `app:showMotionSpec`
`app:hideMotionSpec`
`extendMotionSpec`
`shrinkMotionSpec` | `set*MotionSpec`
`set*MotionSpecResource`
`get*MotionSpec` | see [animators](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/animator) #### Icon attributes   | Attribute | Related method(s) | Default value --------------------------------- | ----------------- | --------------------------------------------------------- | ------------- **Icon** | `app:icon` | `setIcon`
`setIconResource`
`getIcon` | `?attr/colorOnSecondary` **Color** | `app:iconTint` | `setIconTint`
`setIconTintResource`
`getIconTint` | `?attr/colorOnSecondary` (see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/color/mtrl_fab_icon_text_color_selector.xml)) **Size** | `app:iconSize` | `setIconSize`
`getIconSize` | `24dp` **Padding between icon and text** | `app:iconPadding` | `setIconPadding`
`getIconPadding` | `16dp` #### Text label   | Attribute | Related method(s) | Default value -------------- | ------------------------ | --------------------------------- | ------------- **Text label** | `android:text` | `setText`
`getText` | `null` **Color** | `android:textColor` | `setTextColor`
`getTextColor` | `?attr/colorOnSecondary` (see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/color/mtrl_fab_icon_text_color_selector.xml)) **Typography** | `android:textAppearance` | `setTextAppearance` | `?attr/textAppearanceButton` #### Styles   | Style ----------------------------------------- | ----- **Default style** | `Widget.MaterialComponents.ExtendedFloatingActionButton.Icon` **Text-only when**
**extended style** | `Widget.MaterialComponents.ExtendedFloatingActionButton` Default style theme attribute: `?attr/extendedFloatingActionButtonStyle` See the full list of [styles](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/values/styles.xml) and [attrs](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/res/values/attrs.xml). ## Theming FABs ### FAB theming example API and source code: * `FloatingActionButton` * [Class description](https://developer.android.com/reference/com/google/android/material/floatingactionbutton/FloatingActionButton) * [Class source](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/FloatingActionButton.java) * `ExtendedFloatingActionButton` * [Class description](https://developer.android.com/reference/com/google/android/material/floatingactionbutton/ExtendedFloatingActionButton) * [Class source](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/floatingactionbutton/ExtendedFloatingActionButton.java) The following example shows a regular, mini, and extended FABs with Material Theming. !["FAB theming with three FABs - regular, mini and extended - with pink color theming and cut corners."](assets/fabs/fab_theming.png) #### Implementing FAB theming Using theme attributes and styles in `res/values/styles.xml` (themes all FABs and affects other components): ```xml ``` Or using a default style theme attribute, styles and a theme overlay (themes all FABs but does not affect other components): ```xml ``` or using one of the styles in the layout (affects only this FAB): ```xml ```