If you already read the first article regarding Navigation View, you may be wondering what else the new Android Design support library can do for you. In this occasion, I´ll talk about Floating Action Button.


The Floation Action Button (FAB) is a new component included in Material Design guidelines that emphasizes the most important action in our current screen. It´s a bold and stylish way to attract users attention.

Though there is an enormous hype surrounding this component, it´s really important to create FABs only when they are really necessary, and when they show an action that it´s gonna be used quite often (ideally, all the times the activity is shown). It´s interesting to read other voices that affirm FABs are a bad idea.

Floating Action Button

Using Floating Action Button

The use of FAB is really simple, though this component has brought some debate these days after the launch of the design library, because some things didn´t work as expected.

I´ll show you the general behaviour, and then some ideas you might take into account to make it work properly.

First, the only thing you need is use a FloatingActionButton, which extends ImageView, so many of the things you know about that control will be helpful. This is the way you add it to you XML:


As you see, I only added an icon, an elevation (so that it shows a shadow) and a pressedTranslationZ; that way the shadow will grow when it´s pressed. A nice effect, and really easy to add this way. I also added some gravity flags to set it in the bottom right part of the screen. This is supposing you use a FrameLayout. You can adapt it to the layout you use.

Extra options

Those previous customizations are the simplest ones. The FAB will take the color from the accent color defined in the theme, and the ripple color from the colorControlHighlight, though all these things are customizable.

For the background color you could do:


And for the ripple:


You also have methods available to do all this programmatically, but the backgroundTint is a bit more difficult because it use a state list, so this is the way to go (thanks Carlos Morera for the tip):

fab.setBackgroundTintList(ColorStateList.valueOf("Your color"));

There are two different sizes of FAB available, and it´s easy to get the mini version just by adding another property:



Although these things will be probably fixed in future, here are a couple of tricks to make FAB work as expected. First, if you run previous code, you´ll find a lot of different unexpected behaviours depending on the Android version you are using. All of these problems seem to be caused by a property called borderWidth, which you need to set explicitly to 0:


Besides, you will see that with this XML, pre-lollipop versions will get some margin around, while 21+ won´t get it. The reasoning is the same as what you could see on CardView: Shadows on pre-lollipop are rendered using a drawable, which uses its own space to draw it, while the shadows in Lollipop are rendered by the system. CardView had a property that enabled a compat padding.

But Floating Action Button doesn´t. However, this is really easy to simulate. Just add a padding that is dependent on the android version:


You now only need to define the value on its proper dimen.xml files.

For values:


For values-v21:


Easy, right?

Click Event

You can add a click event as usual:

fab.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        Snackbar.make(content, "FAB Clicked", Snackbar.LENGTH_SHORT).show();


Though the impression when using Design Support Library is that it is not perfectly polished, it´s true that with some simple tricks it works quite good and saves a lot of time. I´ll keep investigating new components on future articles. Remember you can see all this code and much more in a small working example at Github.

The team from Webucator has created a video from this article. Take a look!

Thanks for reading.

Author: Antonio Leiva

I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.