The main problem is that there is still no CollapsingToolbarLayout.lock(); method CollapsingToolbarLayout.lock(); (v23.2.1 design support). We hope that this feature will be included in a future version. Until then, I have been proposing the following workaround:
You can collapse and lock the toolbar:
appbar.setExpanded(false,false); CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams(); lp.height = (int) getResources().getDimension(R.dimen.toolbar_height);
and open it:
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams(); lp.height = (int) getResources().getDimension(R.dimen.nav_header_height);
However, there is a problem with the above code:
when the Coordinator is forced to compress. If the title of our toolbar has not disappeared, but CollapsingToolbarLayout.setTitle(CharSequence title); does not work any more. To fix this, we add a TextView to the toolbar and accordingly manage its visibility. (we want it to “go” to the fragment that has the toolbar “unlocked” and “visible” in the fragment that has its toolbar “locked”.
We need to set the TextView android:textAppearance same way as the CollapsingToolbarLayout app:collapsedTitleTextAppearance , in order to be consistent and not disturb the user with different sizes and colors of the toolbar text.
So, with this interface:
public interface ToolbarManipulation { void collapseToolbar(); void expandToolbar(); void setTitle(String s); }
such as:
@Override public void collapseToolbar(){ appbar.setExpanded(false,false); CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams(); lp.height = (int) getResources().getDimension(R.dimen.toolbar_height); toolbarCollapsedTitle.setVisibility(View.VISIBLE); } @Override public void expandToolbar() { CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams(); lp.height = (int) getResources().getDimension(R.dimen.nav_header_height); toolbarCollapsedTitle.setVisibility(View.GONE); } @Override public void setTitle(String s) { collapsingToolbarLayout.setTitle(s); toolbarCollapsedTitle.setText(s); collapsingToolbarLayout.invalidate(); toolbarCollapsedTitle.invalidate(); }
and main xml:
.... <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:collapsedTitleTextAppearance="@style/CollapsedAppBar" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" > <TextView android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:visibility="gone" android:layout_gravity="center_vertical|start" android:gravity="center_vertical|start" android:id="@+id/toolbar_collapsed_title" android:textAppearance="@style/CollapsedAppBar" /> </android.support.v7.widget.Toolbar> </android.support.design.widget.CollapsingToolbarLayout> ......
You can lock and unlock the toolbar as you wish. Your fragments should call these functions in onResume ();
I downloaded an example implementation here .
Note. This is just a workaround, and obviously not the cleanest solution. We are expecting a newer version of com.android.support to solve this problem.