11

I have a coordinator layout containing a collapsing toolbar, a nestedscrollview and a floating button. My floating button is placed at the bottom right end of the collapsing toolbar. I want the button to disappear when the collapsing toolbar is completely collapsed. And be visible in other situation.

My xml :

<?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="fr.djey.secretapp.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:id="@+id/appBar">

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

            <ImageView
                android:id="@+id/collapsing_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                android:scaleType="centerCrop"
                android:src="@drawable/welcome"/>

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

        </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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_gravity="fill_vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/content_frame">

        </FrameLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-60dp"
        android:layout_marginRight="16dp"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/collapse_toolbar"
        app:layout_anchorGravity="bottom|right|end" />

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

Is it possible and how?

Jey10
  • 681
  • 10
  • 30

2 Answers2

17

Try this small change

<android.support.design.widget.FloatingActionButton
    ...
    app:layout_anchor="@id/appBar"
    app:layout_anchorGravity="bottom|right|end" />
Radu Ionescu
  • 3,462
  • 5
  • 24
  • 43
  • Thanks that's perfect! But can you tell me how to check this collapsed state in java so that I can change the anchor state when it's collapsed? – Jey10 Jan 22 '16 at 20:05
  • Have you tried the solution from here [Android Collapsingtoolbarlayout Collapse Listener](http://stackoverflow.com/questions/31682310/android-collapsingtoolbarlayout-collapse-listener) ? – Radu Ionescu Jan 22 '16 at 20:18
  • 1
    Thanks, I try to put that in the java onCreate : appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() But I get a nullpointerexception on this line – Jey10 Jan 22 '16 at 21:44
  • Maybe you forgot to intiliaze the `appBar` view with `findViewById()` or another view you reference in the `onOffsetChanged()` method – Radu Ionescu Jan 22 '16 at 22:58
  • What about if FloatingActionButton is sitting inside one layout (xml) file, in case of fragment, and the AppBar is on different layout's file? – eawedat Oct 03 '19 at 18:28
2

Setting offset listener to the toolbar would give you more flexibility: you'd be able to decide what would happen when the toolbar has collapsed / opened.

    appBarLayout.addOnOffsetChangedListener(
    AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
    if (verticalOffset == 0) {
        showFAB()
        // logic when toolbar is open (for example show fab)
     } else {
        hideFAB()
        // logic when toolbar has collapsed (for example hide fab)
     })

In case you'd like to know whenever toolbar is in between, or if you need the java code: https://stackoverflow.com/a/39424318/11878751

Amir Golan
  • 378
  • 3
  • 8