Android: make CollapsingToolbarLayout header transparent to avoid matching SearchView

I ran into the same problem described in this question https://stackoverflow.com/a/2123609/ (if my search is open and I destroy the toolbar, two overlaps). So I tried to implement an approved answer by making my header transparent when it crashed. The solution only works partially for me, because I have the same problem as described in post n. 11 in the report of this error here . In short, the color of my title does not return to white if the search is open when the toolbar crashes. This is my layout:

<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.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="@dimen/toolbar_expanded_height" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="match_parent" app:expandedTitleMarginEnd="64dp" app:expandedTitleMarginStart="70dp" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:popupTheme="@style/ToolbarPopupTheme" app:layout_collapseMode="pin" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> ... </android.support.design.widget.CoordinatorLayout> 

And this is the code I wrote to listen to searchview open and close:

 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.my_menu, menu); MenuItem searchItem = menu.findItem(R.id.search); MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() { @Override public boolean onMenuItemActionExpand(MenuItem item) { collapsingToolbar.setCollapsedTitleTextColor(Color.TRANSPARENT); return true; } @Override public boolean onMenuItemActionCollapse(MenuItem item) { collapsingToolbar.setCollapsedTitleTextColor(Color.WHITE); return true; } }); ... } 

where collapsingToolbar is obviously a reference to my CollapsingToolbarLayout. Thank you all for your time.

+1
source share
1 answer
  MenuItemCompat.setOnActionExpandListener(menu.findItem(R.id.action_search), new MenuItemCompat.OnActionExpandListener() { @Override public boolean onMenuItemActionExpand(MenuItem item) { mCollapsingToolbar.setCollapsedTitleTextColor(Color.TRANSPARENT); return true; } @Override public boolean onMenuItemActionCollapse(MenuItem item) { mCollapsingToolbar.setCollapsedTitleTextColor(Color.WHITE); mCollapsingToolbar.setTitle(mCategoryName); return false; } }); 

This works great for me. Thanks for your previous posts.

0
source

All Articles