I set FloatingActionButton to bottom of screen and I want to animate the button.
- Hidden when scrolling down
- Shown when scrolling up
Like google implemented it in their Google+ app.
I think CoordinatorLayout and AppBarLayout is needed but how to implement it to use it with the FloatingActionButton?
As of this post, there are no methods that will automatically handle hiding and showing the
FloatingActionButton
in the Design Support Libraries. I know this because this was my first assignment at work.The methods you are thinking of are used to animate the
FloatingActionButton
up and down when aSnackbar
is created, and yes, that will work if you are using aCoordinatorLayout
.Here's my code. It's based off of this repo. It has listeners for
RecyclerView
andAbsListView
that handle animating the button automatically. You can either door
to hide the button manually, or you can call:
and
and it will hide on scroll down and show on scroll up with no further code.
Hope this helps!
AnimatedFloatingActionButton:
AbsListViewScrollDetector:
RecyclerViewScrollDetector:
Googles Design Support Library will do that.
Try to implement this code to your layout file:
It's important that you add
compile 'com.android.support:design:22.2.0'
to your build.gradle. If you're using eclipse there is another way to add this library to your project.Important resources:
Design Support Library (II): Floating Action Button
Android Design Support Library
Google Releasing A FABulous New Design Support Library [Updated]
You can achieve it using the default
FloatingActionButton
changing its defaultBehavior
using theapp:layout_behavior
attribute:You can use a layout like:
With the
app:layout_behavior
you can define your ownBehavior
. With theonStartNestedScroll()
andonNestedScroll()
methods you can interact with scroll events.You can use a Behavior like this. You can find the original code here: