Ralph Bergmann
Ralph Bergmann

Reputation: 3076

disable FAB fade animation

I have this layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="me.myapplication.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_scrolling"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>

When I scroll up and down the FAB fades in and out -> video

Is it possible to disable this fade animation?

Upvotes: 4

Views: 1865

Answers (2)

Ralph Bergmann
Ralph Bergmann

Reputation: 3076

I found it :-)

The Behavior of the FloatingActionButton has a behavior_autoHide option.

The problem was that I tried to set this option in the layout xml. But the FloatingActionButton.Behavior did not read this option because Android calls the wrong constructor.

public Behavior() {
    super();
    mAutoHideEnabled = AUTO_HIDE_DEFAULT;
}

You have to set the Behavior in the layout xml too. Now Android calls the right constructor and the behavior_autoHide flag is read.

public Behavior(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.FloatingActionButton_Behavior_Layout);
    mAutoHideEnabled = a.getBoolean(
            R.styleable.FloatingActionButton_Behavior_Layout_behavior_autoHide,
            AUTO_HIDE_DEFAULT);
    a.recycle();
}

The important part of your layout xml should look like this

<android.support.design.widget.FloatingActionButton
    app:behavior_autoHide="false"
    app:layout_anchor="@id/appbar"
    app:layout_behavior="android.support.design.widget.FloatingActionButton$Behavior"
    app:layout_anchorGravity="bottom|right|end"/> 

Upvotes: 7

Gaket
Gaket

Reputation: 6829

You can try using the full text search: Ctrl+Shift+F to find "FloatingActionButton.Behavior". The successor of this class defines the behavior of FAB, so you can remove lines that handle this fading. If you do not find it, try to find just "Behavior" in case if it is applied to Coordinator layout and not to the FAB directly.

Upvotes: 0

Related Questions