5

I have little problem with overlapping CollapsingToolbarLayout title with SearchView text. When is CollapsingToolbarLayout expanded, there is no problem:

enter image description here

But when is collapsed, the text is overlapped:

enter image description here

How to fix it?

Tomas
  • 4,652
  • 6
  • 31
  • 37
  • 1
    a) The widgets are behaving exactly as they're supposed to. There's no fix. b) Look at how Play Store solved it. The Search action icon expands to a search card overlaying the activity. – Eugen Pechanec Jun 09 '15 at 21:46
  • Thank you for explanation. I think for me now is better expand CollapsingToolbarLayout when search button is clicked, but the code still not working: http://stackoverflow.com/a/30729413/3796931. – Tomas Jun 10 '15 at 21:41
  • @EugenPechanec I don't think that is the correct behaviour, Play Store approach may not be the desired one. Furthermore it's inconstant, my nexus 4 works good, with a nexus 5 doesn't. – Marcel Jun 25 '15 at 10:38
  • 1
    btw I just reported to the issue tracker https://code.google.com/p/android/issues/detail?id=178138&thanks=178138&ts=1435229194 – Marcel Jun 25 '15 at 11:54

3 Answers3

1

The answer is now simple, expand CollapsingToolbarLayout when search button is clicked. Thanks to Tuấn Trần Anh and this code:

coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();

behavior.setTopAndBottomOffset(0);
behavior.onNestedPreScroll(coordinatorLayout, appBarLayout, null, 0, 1, new int[2]);

more information is in this thread.

Community
  • 1
  • 1
Tomas
  • 4,652
  • 6
  • 31
  • 37
  • The bug has been reported to the official bugtracker, marked as assigned and will apparently be fixed in a future update. https://code.google.com/p/android/issues/detail?id=178138&thanks=178138&ts=1435229194 – geecko Jul 11 '15 at 22:29
  • 1
    This doesnt really work, since as soon as user scrolls the appbar collapses again. +1 anyway for getting me on the right path. Check my other solution for how I solved this. – Greg Ennis Aug 19 '15 at 13:04
1

I tried the answer by Tomas, but it had a problem that as soon as the user scrolls, the appbar collapses again and the problem re-appears.

So I came up with another solution which is to make the collapsed title text transparent when the searchview is expanded. This works nicely and does not depend on or change the collapse/expand state of the appbar.

Simply this:

    if (searchViewExpanding) {
        collapsingToolbarLayout.setCollapsedTitleTextColor(Color.TRANSPARENT);
    } else {
        collapsingToolbarLayout.setCollapsedTitleTextColor(Color.WHITE);
    }

Of course, you'll need to handle setOnActionExpandListener of your search menu item to know when to call this.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • I use this solution also, but I forgot to update this page ;) Thank you – Tomas Aug 20 '15 at 13:40
  • Hi Greg, I'm trying to use your solution like this: `MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {public boolean onMenuItemActionExpand(MenuItem item) {collapsingToolbar.setCollapsedTitleTextColor(Color.TRANSPARENT); return true; } public boolean onMenuItemActionCollapse(MenuItem item) { collapsingToolbar.setCollapsedTitleTextColor(Color.WHITE); return true;} });` but if I close my searchview while the toolbar is collapsed, text doesn't return to white. Can you please help me? – MatteoBelfiori Oct 03 '15 at 19:39
  • Im not sure why it doesnt work for you, is the code not being executed when search view collapses? – Greg Ennis Oct 04 '15 at 20:16
  • Hi Greg, I've opened a new question here [link]http://stackoverflow.com/questions/32936050/android-make-collapsingtoolbarlayout-title-transparent-to-avoid-overlap-with-se that better describes my problem. My code should be executed, because in those searchview callbacks I'm saving a boolean that tells if the view is open or not (I use it to retain searchview status between device rotations and it works fine). – MatteoBelfiori Oct 06 '15 at 15:38
-1

EDIT

Still not solve it, they have soleved another realted problem. With changing the text. The trick now is using the ControllableAppLayout to know when the bar is collapsed or expanded so then you just set and empty title setTitle("")

You can find my implementation here https://gist.github.com/skimarxall/863585dcd7abde8f4153

Issue: https://code.google.com/p/android/issues/detail?id=178138

Marcel
  • 2,094
  • 3
  • 23
  • 37