Skip to content

Commit 8598aa6

Browse files
pekingmeleticiarossi
authored andcommitted
[CollapsingToolbarLayout] Fixed incorrect title and subtitle positioning.
PiperOrigin-RevId: 712776905
1 parent 05f8c52 commit 8598aa6

File tree

10 files changed

+138
-97
lines changed

10 files changed

+138
-97
lines changed

catalog/java/io/material/catalog/topappbar/res/layout/cat_topappbar_collapsing_filled_action_fragment.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@
3131
style="?attr/collapsingToolbarLayoutMediumStyle"
3232
android:id="@+id/collapsingtoolbarlayout"
3333
android:layout_width="match_parent"
34-
android:layout_height="?attr/collapsingToolbarLayoutMediumWithSubtitleSize"
34+
android:layout_height="?attr/collapsingToolbarLayoutMediumSize"
3535
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
3636
app:expandedTitleGravity="bottom|center_horizontal"
37-
app:title="@string/cat_topappbar_collapsing_demo_toolbar_medium_title"
38-
app:subtitle="@string/cat_topappbar_collapsing_demo_toolbar_medium_subtitle"
3937
app:toolbarId="@id/toolbar">
4038
<LinearLayout
4139
android:layout_width="match_parent"
@@ -48,7 +46,9 @@
4846
android:layout_width="0dp"
4947
android:layout_height="?attr/actionBarSize"
5048
android:layout_weight="1"
51-
android:elevation="0dp"/>
49+
android:elevation="0dp"
50+
app:title="@string/cat_topappbar_collapsing_demo_toolbar_medium_title"
51+
app:subtitle="@string/cat_topappbar_collapsing_demo_toolbar_medium_subtitle" />
5252
<Button
5353
android:id="@+id/action_button"
5454
android:layout_width="wrap_content"

catalog/java/io/material/catalog/topappbar/res/layout/cat_topappbar_collapsing_large_with_subtitle_fragment.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@
3131
style="?attr/collapsingToolbarLayoutLargeStyle"
3232
android:id="@+id/collapsingtoolbarlayout"
3333
android:layout_width="match_parent"
34-
android:layout_height="?attr/collapsingToolbarLayoutLargeWithSubtitleSize"
34+
android:layout_height="?attr/collapsingToolbarLayoutLargeSize"
3535
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
36-
app:expandedTitleGravity="bottom|center_horizontal"
37-
app:title="@string/cat_topappbar_collapsing_demo_toolbar_large_title"
38-
app:subtitle="@string/cat_topappbar_collapsing_demo_toolbar_large_subtitle">
36+
app:expandedTitleGravity="bottom|center_horizontal">
3937

4038
<androidx.appcompat.widget.Toolbar
4139
android:id="@+id/toolbar"
4240
style="?attr/catalogToolbarWithCloseButtonStyle"
4341
android:layout_width="match_parent"
4442
android:layout_height="?attr/actionBarSize"
4543
android:elevation="0dp"
46-
app:layout_collapseMode="pin"/>
44+
app:layout_collapseMode="pin"
45+
app:title="@string/cat_topappbar_collapsing_demo_toolbar_large_title"
46+
app:subtitle="@string/cat_topappbar_collapsing_demo_toolbar_large_subtitle" />
4747
</com.google.android.material.appbar.CollapsingToolbarLayout>
4848
</com.google.android.material.appbar.AppBarLayout>
4949

catalog/java/io/material/catalog/topappbar/res/layout/cat_topappbar_collapsing_medium_with_subtitle_fragment.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@
3131
style="?attr/collapsingToolbarLayoutMediumStyle"
3232
android:id="@+id/collapsingtoolbarlayout"
3333
android:layout_width="match_parent"
34-
android:layout_height="?attr/collapsingToolbarLayoutMediumWithSubtitleSize"
34+
android:layout_height="?attr/collapsingToolbarLayoutMediumSize"
3535
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
36-
app:expandedTitleGravity="bottom|center_horizontal"
37-
app:title="@string/cat_topappbar_collapsing_demo_toolbar_medium_title"
38-
app:subtitle="@string/cat_topappbar_collapsing_demo_toolbar_medium_subtitle">
36+
app:expandedTitleGravity="bottom|center_horizontal">
3937

4038
<androidx.appcompat.widget.Toolbar
4139
android:id="@+id/toolbar"
4240
style="?attr/catalogToolbarWithCloseButtonStyle"
4341
android:layout_width="match_parent"
4442
android:layout_height="?attr/actionBarSize"
4543
android:elevation="0dp"
46-
app:layout_collapseMode="pin"/>
44+
app:layout_collapseMode="pin"
45+
app:title="@string/cat_topappbar_collapsing_demo_toolbar_medium_title"
46+
app:subtitle="@string/cat_topappbar_collapsing_demo_toolbar_medium_subtitle" />
4747
</com.google.android.material.appbar.CollapsingToolbarLayout>
4848
</com.google.android.material.appbar.AppBarLayout>
4949

catalog/java/io/material/catalog/topappbar/res/layout/cat_topappbar_collapsing_toggleable_action_fragment.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
style="?attr/collapsingToolbarLayoutMediumStyle"
3232
android:id="@+id/collapsingtoolbarlayout"
3333
android:layout_width="match_parent"
34-
android:layout_height="?attr/collapsingToolbarLayoutMediumWithSubtitleSize"
34+
android:layout_height="?attr/collapsingToolbarLayoutMediumSize"
3535
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
3636
app:expandedTitleGravity="bottom|center_horizontal"
3737
app:title="@string/cat_topappbar_collapsing_demo_toolbar_medium_title"

lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java

Lines changed: 97 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ public class CollapsingToolbarLayout extends FrameLayout {
199199
private int extraMultilineHeight = 0;
200200
private boolean extraMultilineHeightEnabled;
201201

202+
private int extraHeightForTitles = 0;
203+
202204
public CollapsingToolbarLayout(@NonNull Context context) {
203205
this(context, null);
204206
}
@@ -664,22 +666,48 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
664666
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
665667
}
666668

667-
if (extraMultilineHeightEnabled && collapsingTitleHelper.getExpandedMaxLines() > 1) {
669+
updateTitleFromToolbarIfNeeded();
670+
671+
if (collapsingTitleEnabled && !TextUtils.isEmpty(collapsingTitleHelper.getText())) {
672+
final int originalHeight = getMeasuredHeight();
668673
// Need to update title and bounds in order to calculate line count and text height.
669-
updateTitleFromToolbarIfNeeded();
670-
updateTextBounds(0, 0, getMeasuredWidth(), getMeasuredHeight(), /* forceRecalculate= */ true);
671-
672-
int lineCount = collapsingTitleHelper.getExpandedLineCount();
673-
if (lineCount > 1) {
674-
// Add extra height based on the amount of height beyond the first line of title text.
675-
int expandedTextHeight =
676-
Math.round(collapsingTitleHelper.getExpandedTextFullSingleLineHeight());
677-
extraMultilineHeight = expandedTextHeight * (lineCount - 1);
678-
int newHeight = getMeasuredHeight() + extraMultilineHeight;
679-
heightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY);
680-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
674+
updateTextBounds(0, 0, getMeasuredWidth(), originalHeight, /* forceRecalculate= */ true);
675+
676+
// Calculates the extra height needed for the contents of the collapsing toolbar, if needed.
677+
int expectedHeight =
678+
(int)
679+
(topInsetApplied
680+
+ expandedMarginTop
681+
+ collapsingTitleHelper.getExpandedTextFullSingleLineHeight()
682+
+ (TextUtils.isEmpty(collapsingSubtitleHelper.getText())
683+
? 0
684+
: expandedTitleSpacing
685+
+ collapsingSubtitleHelper.getExpandedTextFullSingleLineHeight())
686+
+ expandedMarginBottom);
687+
if (expectedHeight > originalHeight) {
688+
extraHeightForTitles = expectedHeight - originalHeight;
681689
} else {
682-
extraMultilineHeight = 0;
690+
extraHeightForTitles = 0;
691+
}
692+
693+
// Calculates the extra height needed for the multiline title, if needed.
694+
if (extraMultilineHeightEnabled && collapsingTitleHelper.getExpandedMaxLines() > 1) {
695+
int lineCount = collapsingTitleHelper.getExpandedLineCount();
696+
if (lineCount > 1) {
697+
// Add extra height based on the amount of height beyond the first line of title text.
698+
int expandedTextHeight =
699+
Math.round(collapsingTitleHelper.getExpandedTextFullSingleLineHeight());
700+
extraMultilineHeight = expandedTextHeight * (lineCount - 1);
701+
} else {
702+
extraMultilineHeight = 0;
703+
}
704+
}
705+
706+
if (extraHeightForTitles + extraMultilineHeight > 0) {
707+
heightMeasureSpec =
708+
MeasureSpec.makeMeasureSpec(
709+
originalHeight + extraHeightForTitles + extraMultilineHeight, MeasureSpec.EXACTLY);
710+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
683711
}
684712
}
685713

@@ -744,35 +772,37 @@ private void updateTextBounds(
744772
updateCollapsedBounds(isRtl);
745773

746774
// Update the expanded bounds
775+
final int titleBoundsLeft = isRtl ? expandedMarginEnd : expandedMarginStart;
776+
final int titleBoundsTop = tmpRect.top + expandedMarginTop;
777+
final int titleBoundsRight =
778+
right - left - (isRtl ? expandedMarginStart : expandedMarginEnd);
779+
final int titleBoundsBottom = bottom - top - expandedMarginBottom;
747780
if (TextUtils.isEmpty(collapsingSubtitleHelper.getText())) {
748781
collapsingTitleHelper.setExpandedBounds(
749-
isRtl ? expandedMarginEnd : expandedMarginStart,
750-
tmpRect.top + expandedMarginTop,
751-
right - left - (isRtl ? expandedMarginStart : expandedMarginEnd),
752-
bottom - top - expandedMarginBottom);
782+
titleBoundsLeft, titleBoundsTop, titleBoundsRight, titleBoundsBottom);
753783

754784
// Now recalculate using the new bounds
755785
collapsingTitleHelper.recalculate(forceRecalculate);
756786
} else {
757787
collapsingTitleHelper.setExpandedBounds(
758-
isRtl ? expandedMarginEnd : expandedMarginStart,
759-
tmpRect.top + expandedMarginTop,
760-
right - left - (isRtl ? expandedMarginStart : expandedMarginEnd),
788+
titleBoundsLeft,
789+
titleBoundsTop,
790+
titleBoundsRight,
761791
(int)
762-
(bottom
763-
- top
764-
- expandedMarginBottom
792+
(titleBoundsBottom
765793
- collapsingSubtitleHelper.getExpandedTextFullSingleLineHeight()
766-
- expandedTitleSpacing));
794+
- expandedTitleSpacing),
795+
/* alignBaselineAtBottom= */ false);
767796
collapsingSubtitleHelper.setExpandedBounds(
768-
isRtl ? expandedMarginEnd : expandedMarginStart,
797+
titleBoundsLeft,
769798
(int)
770-
(tmpRect.top
771-
+ expandedMarginTop
772-
+ collapsingTitleHelper.getExpandedTextFullSingleLineHeight()
799+
(titleBoundsTop
800+
+ (collapsingTitleHelper.getExpandedTextFullSingleLineHeight()
801+
+ extraMultilineHeight)
773802
+ expandedTitleSpacing),
774-
right - left - (isRtl ? expandedMarginStart : expandedMarginEnd),
775-
bottom - top - expandedMarginBottom);
803+
titleBoundsRight,
804+
titleBoundsBottom,
805+
/* alignBaselineAtBottom= */ false);
776806

777807
// Now recalculate using the new bounds
778808
collapsingTitleHelper.recalculate(forceRecalculate);
@@ -785,9 +815,15 @@ private void updateTextBounds(
785815
private void updateTitleFromToolbarIfNeeded() {
786816
if (toolbar != null) {
787817
if (collapsingTitleEnabled) {
788-
if (TextUtils.isEmpty(collapsingTitleHelper.getText())) {
818+
CharSequence title = getToolbarTitle(toolbar);
819+
if (TextUtils.isEmpty(collapsingTitleHelper.getText()) && !TextUtils.isEmpty(title)) {
789820
// If we do not currently have a title, try and grab it from the Toolbar
790-
setTitle(getToolbarTitle(toolbar));
821+
setTitle(title);
822+
}
823+
CharSequence subtitle = getToolbarSubtitle(toolbar);
824+
if (TextUtils.isEmpty(collapsingSubtitleHelper.getText()) && !TextUtils.isEmpty(subtitle)) {
825+
// If we do not currently have a subtitle, try and grab it from the Toolbar
826+
setSubtitle(subtitle);
791827
}
792828
}
793829
}
@@ -819,31 +855,24 @@ private void updateCollapsedBounds(boolean isRtl) {
819855
titleMarginTop = 0;
820856
titleMarginBottom = 0;
821857
}
858+
final int titleBoundsLeft = tmpRect.left + (isRtl ? titleMarginEnd : titleMarginStart);
859+
final int titleBoundsRight = tmpRect.right - (isRtl ? titleMarginStart : titleMarginEnd);
860+
final int titleBoundsTop = tmpRect.top + maxOffset + titleMarginTop;
861+
final int titleBoundsBottom = tmpRect.bottom + maxOffset - titleMarginBottom;
822862
if (TextUtils.isEmpty(collapsingSubtitleHelper.getText())) {
823863
collapsingTitleHelper.setCollapsedBounds(
824-
tmpRect.left + (isRtl ? titleMarginEnd : titleMarginStart),
825-
tmpRect.top + maxOffset + titleMarginTop,
826-
tmpRect.right - (isRtl ? titleMarginStart : titleMarginEnd),
827-
tmpRect.bottom + maxOffset - titleMarginBottom);
864+
titleBoundsLeft, titleBoundsTop, titleBoundsRight, titleBoundsBottom);
828865
} else {
829866
collapsingTitleHelper.setCollapsedBounds(
830-
tmpRect.left + (isRtl ? titleMarginEnd : titleMarginStart),
831-
tmpRect.top + maxOffset + titleMarginTop,
832-
tmpRect.right - (isRtl ? titleMarginStart : titleMarginEnd),
833-
(int)
834-
(tmpRect.bottom
835-
+ maxOffset
836-
- titleMarginBottom
837-
- collapsingSubtitleHelper.getCollapsedFullSingleLineHeight()));
867+
titleBoundsLeft,
868+
titleBoundsTop,
869+
titleBoundsRight,
870+
(int) (titleBoundsBottom - collapsingSubtitleHelper.getCollapsedFullSingleLineHeight()));
838871
collapsingSubtitleHelper.setCollapsedBounds(
839-
tmpRect.left + (isRtl ? titleMarginEnd : titleMarginStart),
840-
(int)
841-
(tmpRect.top
842-
+ maxOffset
843-
+ titleMarginTop
844-
+ collapsingTitleHelper.getCollapsedFullSingleLineHeight()),
845-
tmpRect.right - (isRtl ? titleMarginStart : titleMarginEnd),
846-
tmpRect.bottom + maxOffset - titleMarginBottom);
872+
titleBoundsLeft,
873+
(int) (titleBoundsTop + collapsingTitleHelper.getCollapsedFullSingleLineHeight()),
874+
titleBoundsRight,
875+
titleBoundsBottom);
847876
}
848877
}
849878

@@ -858,6 +887,17 @@ private static CharSequence getToolbarTitle(View view) {
858887
}
859888
}
860889

890+
@Nullable
891+
private static CharSequence getToolbarSubtitle(View view) {
892+
if (view instanceof androidx.appcompat.widget.Toolbar) {
893+
return ((androidx.appcompat.widget.Toolbar) view).getSubtitle();
894+
} else if (view instanceof android.widget.Toolbar) {
895+
return ((android.widget.Toolbar) view).getSubtitle();
896+
} else {
897+
return null;
898+
}
899+
}
900+
861901
private static int getHeightWithMargins(@NonNull final View view) {
862902
final ViewGroup.LayoutParams lp = view.getLayoutParams();
863903
if (lp instanceof MarginLayoutParams) {
@@ -1838,7 +1878,10 @@ public void setScrimVisibleHeightTrigger(@IntRange(from = 0) final int height) {
18381878
public int getScrimVisibleHeightTrigger() {
18391879
if (scrimVisibleHeightTrigger >= 0) {
18401880
// If we have one explicitly set, return it
1841-
return scrimVisibleHeightTrigger + topInsetApplied + extraMultilineHeight;
1881+
return scrimVisibleHeightTrigger
1882+
+ topInsetApplied
1883+
+ extraMultilineHeight
1884+
+ extraHeightForTitles;
18421885
}
18431886

18441887
// Otherwise we'll use the default computed value

lib/java/com/google/android/material/appbar/res-public/values/public.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
<public name="collapsingToolbarLayoutMediumStyle" type="attr"/>
2121
<public name="collapsingToolbarLayoutLargeStyle" type="attr"/>
2222
<public name="collapsingToolbarLayoutMediumSize" type="attr"/>
23-
<public name="collapsingToolbarLayoutMediumWithSubtitleSize" type="attr"/>
2423
<public name="collapsingToolbarLayoutLargeSize" type="attr"/>
25-
<public name="collapsingToolbarLayoutLargeWithSubtitleSize" type="attr"/>
2624
<public name="toolbarSurfaceStyle" type="attr"/>
2725
<public name="layout_scrollInterpolator" type="attr"/>
2826
<public name="layout_scrollEffect" type="attr"/>

lib/java/com/google/android/material/appbar/res/values/attrs.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,9 @@
3131
<!-- Size to use for Medium CollapsingToolbarLayouts in this theme. -->
3232
<attr name="collapsingToolbarLayoutMediumSize" format="reference"/>
3333

34-
<!-- Size to use for Medium CollapsingToolbarLayouts with subtitle in this theme. -->
35-
<attr name="collapsingToolbarLayoutMediumWithSubtitleSize" format="reference"/>
36-
3734
<!-- Size to use for Large CollapsingToolbarLayouts in this theme. -->
3835
<attr name="collapsingToolbarLayoutLargeSize" format="reference"/>
3936

40-
<!-- Size to use for Large CollapsingToolbarLayouts with subtitle in this theme. -->
41-
<attr name="collapsingToolbarLayoutLargeWithSubtitleSize" format="reference"/>
42-
4337
<!-- Style to use for Surface Toolbars in this theme. -->
4438
<attr name="toolbarSurfaceStyle" format="reference"/>
4539

lib/java/com/google/android/material/dialog/res/values/themes_base.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,7 @@
178178

179179
<!-- Top App Bars. -->
180180
<item name="collapsingToolbarLayoutMediumSize">@dimen/m3_appbar_size_medium</item>
181-
<item name="collapsingToolbarLayoutMediumWithSubtitleSize">@dimen/m3_appbar_size_medium_with_subtitle</item>
182181
<item name="collapsingToolbarLayoutLargeSize">@dimen/m3_appbar_size_large</item>
183-
<item name="collapsingToolbarLayoutLargeWithSubtitleSize">@dimen/m3_appbar_size_large_with_subtitle</item>
184182

185183
<!-- Action Bars. -->
186184
<item name="actionBarPopupTheme">@style/ThemeOverlay.Material3.Light</item>
@@ -471,9 +469,7 @@
471469

472470
<!-- Top App Bars. -->
473471
<item name="collapsingToolbarLayoutMediumSize">@dimen/m3_appbar_size_medium</item>
474-
<item name="collapsingToolbarLayoutMediumWithSubtitleSize">@dimen/m3_appbar_size_medium_with_subtitle</item>
475472
<item name="collapsingToolbarLayoutLargeSize">@dimen/m3_appbar_size_large</item>
476-
<item name="collapsingToolbarLayoutLargeWithSubtitleSize">@dimen/m3_appbar_size_large_with_subtitle</item>
477473

478474
<!-- Action Bars. -->
479475
<item name="actionBarPopupTheme">@style/ThemeOverlay.Material3.Dark</item>

0 commit comments

Comments
 (0)