title: Stop CollapsingToolbar from collapsing after NestedScrollView runs out of content to scroll
body: <p>In Android, how can I get the CollapsingToolbar to stop collapsing if the NestedScrollView runs out of content to scroll? This functionality currently exists in the Contacts app on Android 5.1.1. However, in my code when the NestedScrollView stops scrolling the toolbar continues to collapse leaving gap between the two.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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:id="@+id/main_content"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
      &lt;android.support.design.widget.AppBarLayout
          android:id="@+id/appbar"
          android:layout_width="match_parent"
          android:layout_height="256dp"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"&gt;
          &lt;android.support.design.widget.CollapsingToolbarLayout
              android:id="@+id/collapsing_toolbar"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_scrollFlags="scroll|exitUntilCollapsed"
              app:contentScrim="?attr/colorPrimary"
              app:expandedTitleMarginStart="@dimen/content_padding_normal"
              app:expandedTitleMarginEnd="64dp"&gt;
              &lt;android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  app:titleTextAppearance="@style/ActionBar.TitleText"
                  app:layout_collapseMode="pin" /&gt;
          &lt;/android.support.design.widget.CollapsingToolbarLayout&gt;
      &lt;/android.support.design.widget.AppBarLayout&gt;
      &lt;android.support.v4.widget.NestedScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"
          android:scrollbars="none"&gt;
          &lt;LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:paddingBottom="@dimen/keyline_2"&gt;
              &lt;android.support.v7.widget.CardView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="@dimen/element_spacing_normal"&gt;
                  &lt;include
                      layout="@layout/ViewLoadingIndeterminate" /&gt;
                  &lt;LinearLayout
                      android:id="@+id/progress_status_container"
                      style="@style/ConnectionFieldContainer"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center_vertical"
                      android:orientation="vertical"
                      android:visibility="visible"&gt;
                      &lt;Spinner
                          android:id="@+id/progress_status"
                          android:layout_width="match_parent"
                          style="@style/Text.ConnectionField" /&gt;
                      &lt;TextView
                          style="@style/Text.ConnectionLabel"
                          android:text="@string/mobile.customer.connect.progress.status" /&gt;
                  &lt;/LinearLayout&gt;
              &lt;/android.support.v7.widget.CardView&gt;
              &lt;android.support.v7.widget.CardView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="@dimen/element_spacing_normal"&gt;
                  &lt;LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="vertical"&gt;
                      &lt;LinearLayout
                          android:id="@+id/email1_container"
                          style="@style/ConnectionFieldContainer"
                          android:orientation="horizontal"
                          tools:visibility="visible"&gt;
                          &lt;LinearLayout
                              android:layout_width="0dp"
                              android:layout_height="wrap_content"
                              android:layout_gravity="center_vertical"
                              android:layout_weight="1"
                              android:orientation="vertical"&gt;
                              &lt;TextView
                                  android:id="@+id/email1"
                                  style="@style/Text.ConnectionField"
                                  tools:text="bgnosis@gmail.com" /&gt;
                              &lt;TextView
                                  style="@style/Text.ConnectionLabel"
                                  android:text="@string/mobile.customer.connect.email1" /&gt;
                          &lt;/LinearLayout&gt;
                          &lt;ImageButton
                              android:id="@+id/action_email1"
                              style="@style/Button.ConnectionAction"
                              android:src="@drawable/ic_email_black_24dp" /&gt;
                      &lt;/LinearLayout&gt;
                      &lt;LinearLayout
                          android:id="@+id/email2_container"
                          style="@style/ConnectionFieldContainer"
                          android:orientation="horizontal"
                          tools:visibility="visible"&gt;
                          &lt;LinearLayout
                              android:layout_width="0dp"
                              android:layout_height="wrap_content"
                              android:layout_gravity="center_vertical"
                              android:layout_weight="1"
                              android:orientation="vertical"&gt;
                              &lt;TextView
                                  android:id="@+id/email2"
                                  style="@style/Text.ConnectionField"
                                  tools:text="alternate@email.com" /&gt;
                              &lt;TextView
                                  style="@style/Text.ConnectionLabel"
                                  android:text="@string/mobile.customer.connect.email2" /&gt;
                          &lt;/LinearLayout&gt;
                          &lt;ImageButton
                              android:id="@+id/action_email2"
                              style="@style/Button.ConnectionAction"
                              android:src="@drawable/ic_email_black_24dp" /&gt;
                      &lt;/LinearLayout&gt;
                      &lt;LinearLayout
                          android:id="@+id/phone_day_container"
                          style="@style/ConnectionFieldContainer"
                          android:orientation="horizontal"
                          tools:visibility="visible"&gt;
                          &lt;LinearLayout
                              android:layout_width="0dp"
                              android:layout_height="wrap_content"
                              android:layout_gravity="center_vertical"
                              android:layout_weight="1"
                              android:orientation="vertical"&gt;
                              &lt;TextView
                                  android:id="@+id/phone_day"
                                  style="@style/Text.ConnectionField"
                                  tools:text="801-555-1234" /&gt;
                              &lt;TextView
                                  style="@style/Text.ConnectionLabel"
                                  android:text="@string/mobile.customer.connect.phone.day" /&gt;
                          &lt;/LinearLayout&gt;
                          &lt;ImageButton
                              android:id="@+id/action_call_phone_day"
                              style="@style/Button.ConnectionAction"
                              android:src="@drawable/ic_call_black_24dp" /&gt;
                          &lt;ImageButton
                              android:id="@+id/action_text_phone_day"
                              style="@style/Button.ConnectionAction"
                              android:src="@drawable/ic_textsms_black_24dp" /&gt;
                      &lt;/LinearLayout&gt;
          &lt;/LinearLayout&gt;
      &lt;/android.support.v4.widget.NestedScrollView&gt;
      &lt;android.support.design.widget.FloatingActionButton
          android:id="@+id/create_reminder"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_anchor="@id/collapsing_toolbar"
          app:layout_anchorGravity="bottom|right|end"
          app:borderWidth="0dp"
          app:elevation="@dimen/shadow_size"
          android:layout_marginBottom="@dimen/keyline_1"
          android:layout_marginRight="@dimen/keyline_1"
          android:src="@drawable/ic_alarm_add_white_24dp"
          app:backgroundTint="?attr/colorAccent" /&gt;
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/fUsfa.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fUsfa.png" alt="example"></a></p>