2

I'm facing the same problem described in this SO question (if my searchview is open and I collapse my toolbar, the two overlaps). So I was trying to implement the approved answer, making my title transparent when it is collapsed. Solution is only partially working for me, because I'm having the same problem described in the message n. 11 in the report of this bug here. In short, the color of my title doesn't return to white if the searchview is open when the toolbar is collapsed. 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've wrote to listen 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 the reference to my CollapsingToolbarLayout. Thank you all for your time.

Community
  • 1
  • 1
MatteoBelfiori
  • 280
  • 3
  • 13
  • Did you tried the last work around? [#15](https://code.google.com/p/android/issues/detail?id=178138#c15) – Mario Velasco Oct 04 '15 at 22:40
  • Yes, I've tried it but apparently it doesn't work for me. I've used `collapsingToolbar.setTitle("Title");` just before the `return true;` of the `onMenuItemActionCollapse` callback. No luck. – MatteoBelfiori Oct 05 '15 at 19:10

1 Answers1

1
 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 fine for me. Thanks for your previous posts

nadafafif
  • 561
  • 6
  • 10
  • Hi nadafafif, thanks for your answer, but this solution is basically the one that Mario Velasco suggested me and it doesn't work for me. Besides, returning false in `onMenuItemActionCollapse()` doesn't let my searchview to close back, so I don't understand why it's working for you. – MatteoBelfiori Oct 07 '15 at 15:10