Floating action buttons
Floating action buttons
Another component you will often see is the floating action button, or FAB. You can see one in
Figure 35.11.
Figure 35.11 A floating action button
An implementation of the floating action button is available in Google’s design support
library. You can include this library in your project with this dependency on your module:
com.android.support:design:24.2.1.
Floating action buttons are little more than a solid-color circle with a custom circular shadow, provided
by an OutlineProvider. The FloatingActionButton class, a subclass of ImageView, takes care of the
circle and shadow for you. Simply place a FloatingActionButton in your layout file and set its src
attribute to the image that you want to display in your button.
While you could place your floating action button in a FrameLayout, the design support library also
includes the clever CoordinatorLayout. This layout is a subclass of FrameLayout that changes your
floating action button’s position based on the movement of other components. Now, when you display
a Snackbar (more on that in a moment), your FAB will move up so that the Snackbar does not cover it.
This will look like Listing 35.10.