Imagine you’re scrolling down a very long screen (this is a common situation with Privacy Policies). As you scroll down the ‘Accept’ button scrolls off the screen. This is a short but helpful Android Tutorial for fixing this UX problem.

I will show you how to place the button in an alternative position: the bottom of the screen. When the original button reappears, the button at the bottom should disappear (which means that you should never have both buttons visible on the screen).

It shoud look like this:

The support library provides us with two components that are very useful for this Android tutorial:

– The button at the bottom of the screen can be implemented with the help of a Bottom Sheet component.

– For identifying the scrolling behavior and triggering the appearance of the Bottom Sheet, we can use a Coordinator Layout.

Android Authority has published two quite helpful tutorials, with source code on Github for these two components:

The layout consists of a CoordinatorLayout that has two children: a ScrollView with the scrollable content and a FrameLayout with the bottom button. We can now manually show/hide the bottom sheet by calling

bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED)

or

bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN).

But of course we don’t want to bring the Bottom Sheet up manually, we want it to appear automatically when the rounded button disappears off the screen. The second tutorial of Android Authority that we have linked above demonstrates in case 4, how we can implement a custom behavior and alter a view’s attributes when another widget goes off the screen.
So we could implement a custom CoordinatorLayout.Behavior following the tutorial, and, instead of toggling the button’s child, set the Bottom sheet’s state to expanded.

But, wait… then we would have to add this custom CoordinatorLayout.Behavior to the bottom_sheet FrameLayout, but this already has a coordinator layout set. And we can’t add two behaviors.

The solution is simple. We just need to extend the BottomSheetBehavior as in the snippet below: (we don’t even have to resort to a Decorator — simple inheritance works).

The implementation is really simple, we just get the position of the coordinator’s dependency, and as soon as it hides behind the status bar, we set the state to STATE_EXPANDED by calling the parent’s setState method.

A few notes about the code above:

The code uses a somewhat unsafe method of calculating the status bar’s height. There are countless questions on StackOverflow about how to get the status bar’s height. I consider this to be the cleaner one. If you want to avoid it, you might be tempted to just trigger the action when the y-location of the dependency on screen is 0. Keep in mind though that if your status bar is not translucent, the position will never be 0.

For all this to work, there is a small addition that we need to do to the layout code in order for the whole thing to work.

The CoordinatorLayout only “coordinates” its direct children, so we can’t actually make the behavior directly depend on the rounded_button. The trick to overcome this limitation is to add the dummy View behavior_dependency, which is anchored to the rounded_button and so moves in sync with it, and then use this view as the dependency view in our Custom behavior.