material-components / material-components/material-components-android

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

Open Beginner friendly
#5,094 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
17.4k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

**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 |
| ---------------- | ---------------- |
| | |

**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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at CollapsingToolbarLayout.updateCollapsedBounds around line 953 and compare the entireSpace branch with the regular dummy-view calculation. Use the linked minimal sample app to reproduce a delayed requestLayout during the Slide transition; done means the collapsed title remains at the toolbar's leading edge, matching the expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
design, mobile
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.