You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When CollapsingToolbarLayout with app:collapsedTitleGravityMode="entireSpace" is laid out during transition, collapsed title receives incorrect bounds, resulting in the title being rendered away from the leading edge of the toolbar.
Expected behavior:
Expected
Actual
expected.mp4
actual.mp4
Source code:
In CollapsingToolbarLayout.updateCollapsedBounds:953 collapsed bounds for offsets are calculated for COLLAPSED_TITLE_GRAVITY_ENTIRE_SPACE. The comment states: "If the collapsed title gravity should be using the whole collapsing toolbar layout instead of the dummy layout, we should set the collapsed bounds for offsets.". It seems like for parity, DescendantOffsetUtils.getDescendantRect(this, this, tmpRect); has been used to calculate tmpRect as in the regular case (DescendantOffsetUtils.getDescendantRect(this, dummyView, tmpRect); on line 902).
This however results in calculating the position of CollapsingToolbarLayout relative to the entire window as the function traverses up from the view (second argument - this) to the parent (first argument - this) starting from the parent of the second argument (this.getParent()). The view itself will never be its own parent therefore the traversal will be performed up to the window. As getDescendantRect includes all transformations, any translation applied by the transition (e.g. Slide(Gravity.RIGHT)) to the fragment's view will be included in the calculation resulting in the incorrect layout.
Suggested fix is to use tmpRect.set(0, 0, getWidth(), getHeight(); instead.
The reproduction app adds a new fragment with CollapsingToolbarLayout with androidx.transition.Slide transition. When first button is used, layout is requested synchronously. When second button is used, requestLayout is delayed by 150 ms, simulating layout performed during transition. This results in collapsed title being laid out incorrectly.
Description:
When
CollapsingToolbarLayoutwithapp:collapsedTitleGravityMode="entireSpace"is laid out during transition, collapsed title receives incorrect bounds, resulting in the title being rendered away from the leading edge of the toolbar.Expected behavior:
expected.mp4
actual.mp4
Source code:
In
CollapsingToolbarLayout.updateCollapsedBounds:953collapsed bounds for offsets are calculated forCOLLAPSED_TITLE_GRAVITY_ENTIRE_SPACE. The comment states: "If the collapsed title gravity should be using the whole collapsing toolbar layout instead of the dummy layout, we should set the collapsed bounds for offsets.". It seems like for parity,DescendantOffsetUtils.getDescendantRect(this, this, tmpRect);has been used to calculatetmpRectas in the regular case (DescendantOffsetUtils.getDescendantRect(this, dummyView, tmpRect);on line 902).This however results in calculating the position of
CollapsingToolbarLayoutrelative to the entire window as the function traverses up from the view (second argument -this) to the parent (first argument -this) starting from the parent of the second argument (this.getParent()). The view itself will never be its own parent therefore the traversal will be performed up to the window. AsgetDescendantRectincludes all transformations, any translation applied by the transition (e.g.Slide(Gravity.RIGHT)) to the fragment's view will be included in the calculation resulting in the incorrect layout.Suggested fix is to use
tmpRect.set(0, 0, getWidth(), getHeight();instead.Minimal sample app repro: https://github.com/kligarski/android-collapsing-toolbar-layout-title-bug
The reproduction app adds a new fragment with
CollapsingToolbarLayoutwithandroidx.transition.Slidetransition. When first button is used, layout is requested synchronously. When second button is used,requestLayoutis delayed by 150 ms, simulating layout performed during transition. This results in collapsed title being laid out incorrectly.Android API version: API 37
Material Library version: 1.14.0
Device: Pixel 10 Pro API 37 emulator
Happy to open a PR if helpful.