CoordinatorLayout / DesignLibrary / FloatingActionButton / Snackbar

Design Library – Part 4

At Google I/O 2015 the Material Design Support Library was announced, and with it creating material apps targeting API levels below API 19 suddenly got a lot easier. In this series we’ll look at taking the RSS Reader app that we used as a basis for the Material series, and re-write it to make full use of the new Design Support Library. In this article we’ll look at adding a Floating Action Button or FAB.

Before we begin it is worth pointing out that this app really does not need a FAB because there are no actions to perform. However, in order to show how easy it now easy it is to add a FAB I took inspiration from Douglas Adams:

“Arthur Dent: What happens if I press this button?
Ford Prefect: I wouldn’t-
Arthur Dent: Oh.
Ford Prefect: What happened?
Arthur Dent: A sign lit up, saying ‘Please do not press this button again.”

― Douglas Adams, The Original Hitchhiker Radio Scripts

So we’ll implement a FAB which, when pressed, pops up a Snackbar saying “Please do not press this button again.”

The first thing we need to do is add a FloatingActionButton to our layout:

Before we do anything more, let’s see how that looks:

Part4-FAB-white

Oh noes! Our FAB is white and looks pretty fugly. Remember when we we having problems with the indicator bar colour in our TabLayout and we set it to white to match the text? Guess what? The same accent colour is used for the FAB colour. However it’s easily fixed, let’s use an accent colour which works in both cases:

That’s much better:

Part4-FAB-green

All that remains is to wire up the OnClickListener in our Activity:

So we can now try this out:

Notice how the FAB moves up with the Snackbar? We had to nothing to get that behaviour, so what’s happening? It’s our friend CoordinatorLayout doing stuff in the background, once again.

When the Snackbar is created we pass in a reference to a View – in this case it’s the FAB which generates the onClick() call. Snackbar looks at the parent layout of this View and, if it finds a CoordinatorLayout it registers its behaviour with the CoordinatorLayout. FloatingActionButton also registers it’s own behaviour with the parent CoordinatorLayout which has a dependency on the Snackbar behaviour, and CoordinatorLayout passes the relevant information across when the Snackbar scrolls in and out. It’s even intelligent enough that, on a larger device where the Snackbar does not stretch the full width, it does not move the FAB because it doesn’t need to.

Pretty neat, and we didn’t have to do anything!

For proof of the magic that CoordinatorLayout performs simply pass a View outside of CoordinatorLayout to the Snackbar.make() method and you’ll see this nice behaviour cease.

There is one issue that really needs mentioning here. If you run this code on an Android 4.x device the FAB rendering is pretty poor:

Part4-FAB-4.x

However there is a really simple work-around for this simply add app:borderWidth="0dp" to your FloatingActionButton in the layout and everything looks fine:

Part4-FAB-4.x-fixed

This fix came from Chris Banes via Sebastiano Poggi so I’m guessing that it will be fixed in a future version of the design support library. But thanks to Seb & Chris for providing me with such a simple work-around.

With the exception of this final issue, all of the example pictures and videos have been from Lollipop devices and so we’d expect a full material experience, but how do things fare on devices running older versions of Android? In the next article we’ll look at some subtleties regarding backwards compatibility.

The source code for this article is available here.

© 2015, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Design Library – Part 4 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

Leave a Reply

Your email address will not be published. Required fields are marked *