Scroll your Bottom Navigation View away with 10 lines of code

Like many things material design Google introduced Bottom navigation bars on Android via the design library but failed to provide one key component — a scroll behavior for them. This is frustrating because the official material design specs specify that the bottom bar can hide on scroll. For my app, Curated I needed this behavior.

Google+ app bottom scroll bar hiding

A custom behavior — surprisingly easy

I knew the implementation would involve writing a custom CoordinatorLayout Behavior which is what threw me off initially. Anytime I’ve delved into the example code of custom behaviors it seemed way too complicated. It turns out, though, that in this case it was not that hard at all:

I’d call that on par with what we are aiming for

Attaching the behaviour to your view

If you’ve never used a custom CoordinatorLayout behavior the way you attach it to your view is like this — just put in layout_behavourthe path to your own class:

What about Snackbars?

What we have is great but it has a flaw — it doesn’t respect Snackbars appearing in the CoordinatorLayout. If one was to appear it’ll be behind the BottomNavigationView which is wrong. The material specs are clear on what should happen:

Snackbars should appear above the Bottom bar

What about Floating Action Buttons (FAB)?

Another element to handle with this BottomNavigationView implementation is Floating Action Buttons.

What about snap behaviour?

By popular demand I’ve decided to include how to extend the BottomNavigationBehavior to handle snapping. By snapping I mean a behavior similar to what you get when you add app:layout_scrollFlags=”snap” to your AppBarLayout views — for example to your Toolbar. It makes it so the Toolbar snaps to either be hidden or visible when the user stops scrolling.

  1. If so, it checks if the child should be snapped to full visibility or to a hidden state. It does this by checking how much of the child is currently showing.
  2. It then animates the child’s offset to that new state.

I'm passionate about building applications which are both well designed and intelligently architected. Find me at https://valcanbuild.tech

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store