Search code examples
androidandroid-toolbarandroid-design-libraryfloating-action-buttonandroiddesignsupport

FAB doesn't scroll under Toolbar, when anchored to a TextView, using the new Design-Support Library


I am using the new support-design library. As you can see by the gif and code below, I have a standard setup for a expanding/collapsing toolbar that contains an imageview.

Below the appbar, I have a NestedScrollView that contains a textview among other things. As you can see at the very bottom of the code, I have a FAB, anchored to the textview. I want the FAB to scroll under the Toolbar.

Any help would be much appreciated. Thanks.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            app:layout_scrollFlags="exitUntilCollapsed|scroll"
            app:expandedTitleTextAppearance="@style/TransparentText"
            app:collapsedTitleTextAppearance="@style/TransparentText">

            <ImageView
                android:id="@+id/venue_image"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_gravity="top"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:actionBarSize"
                android:minHeight="?actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_collapseMode="parallax"
        app:layout_scrollFlags="scroll">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#F2F2F2"
            android:orientation="vertical">

            <TextView
                android:id="@+id/test_textview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="48dp"
                android:layout_gravity="bottom"
                android:background="@color/primary"
                android:minHeight="?android:actionBarSize"
                android:paddingBottom="16dp"
                android:paddingRight="16dp"
                android:paddingTop="16dp"
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit"
                android:textColor="@android:color/white"
                android:textSize="@dimen/expanded_toolbar_text"/>

         </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <!-- Anchored to @+id/test_textview-->
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:clickable="true"
        android:src="@drawable/ic_heart_white_24dp"
        app:fabSize="normal"
        app:layout_anchor="@+id/toolbar_textview"
        app:layout_anchorGravity="bottom|right|end" />

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

enter image description here


Solution

  • Try this:

       <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:clickable="true"
        android:src="@drawable/ic_heart_white_24dp"
        app:fabSize="normal"
        app:layout_anchor="@+id/app_bar_layout"
        app:layout_anchorGravity="bottom|right|end" />
    

    I replaced the anchor @+id/toolbar_textview with @+id/app_bar_layout, It is the right approach that should work.