tothkris tothkris - 1 year ago 301
Android Question

Android Floating Action Button animation

All I want is a simple animation, when the scrollview moves. I have tried some solution, but none of them worked perfectly/smoothly.If I scroll, I want to hide the fab with slide down animation and if nothing happens after 2 sec the fab shows with slide up animation. I know it is a basic question and I appreciate your patientence.

Thanks in advance.

final ScrollView scroll = (ScrollView) v.findViewById(;
scroll.setOnTouchListener(new View.OnTouchListener()){
public boolean onTouch(View v, Motionevent event){
int action = event.getAction();
if (action == Motionevent.ACTION_MOVE){

//slide down animation here

new Handler().postDelayed(new Runnable(){
public void run(){

//slide up animation here

}, 2000);
return false;

Answer Source

Here's a tutorial , how to use the FAB button with scroll animation.


  1. Use the v22.2.1 support v4 library, there is a show() and hide() method that performs the fade-in and fade-out animations for Floating Action Buttons
  2. You must place your ScrollView and the FAB inside a CoordinatorLayout.
  3. Set the FAB layout_anchor to the ScrollView's id
  4. Create a class and extend the FloatingActionButton.Behavior class and set it to the FAB's layout_behavior attribute in the layout xml
  5. Override your Behavior class onStartNestedScroll to check the is vertical
  6. Override your Behavior class onStopNestedScroll to call the child's(FAB) parameter hide() method on downscroll and postDelay a Runnable to show the FAB after 2 seconds

layout like:

... >

I suggest, to also create a Handler into the Behavior class to call the FAB's show() method. Behavior class like (not tested):

public class CustomScrollAwareBehavior extends FloatingActionButton.Behavior{

private Handler handler = new Handler();
private FloatingActionButton fab;

public CustomScrollAwareBehavior(Context context, AttributeSet attrs) {

public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
                                   FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
    fab = child;
    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||
            super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,

Runnable showRunnable = new Runnable() {
    public void run() {;

public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child,
                           View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
     if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download