Skip to content

[CollapsingToolbarLayout] collapsed title with gravity mode enitreSpace has incorrect bounds when layout is performed during transition #5094

Description

@kligarski

Description:

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.

Minimal sample app repro: https://github.com/kligarski/android-collapsing-toolbar-layout-title-bug

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.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions