Skip to content

Commit 57297ae

Browse files
pekingmeleticiarossi
authored andcommitted
[CollapsingToolbarLayout] Added multiple subtitle support.
PiperOrigin-RevId: 713365011
1 parent 8a4d3c6 commit 57297ae

File tree

5 files changed

+59
-20
lines changed

5 files changed

+59
-20
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
android:layout_width="match_parent"
3636
android:layout_height="?attr/collapsingToolbarLayoutLargeSize"
3737
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
38-
app:maxLines="3">
38+
app:titleMaxLines="3"
39+
app:subtitleMaxLines="3">
3940

4041
<androidx.appcompat.widget.Toolbar
4142
android:id="@+id/toolbar"
@@ -44,8 +45,9 @@
4445
android:layout_height="?attr/actionBarSize"
4546
android:elevation="0dp"
4647
app:layout_collapseMode="pin"
48+
tools:ignore="UnusedAttribute"
4749
app:title="@string/cat_topappbar_collapsing_multiline_demo_toolbar_title"
48-
tools:ignore="UnusedAttribute" />
50+
app:subtitle="@string/cat_topappbar_collapsing_multiline_demo_toolbar_subtitle" />
4951
</com.google.android.material.appbar.CollapsingToolbarLayout>
5052
</com.google.android.material.appbar.AppBarLayout>
5153

catalog/java/io/material/catalog/topappbar/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
<!-- Sample long title [CHAR_LIMIT=NONE] -->
8686
<string name="cat_topappbar_collapsing_multiline_demo_toolbar_title" description="Long title [CHAR_LIMIT=NONE]">This Collapsing Title is
8787
extremely long and thus will be displayed in multiple lines.</string>
88+
<string name="cat_topappbar_collapsing_multiline_demo_toolbar_subtitle" description="Long subtitle [CHAR_LIMIT=NONE]">This Collapsing Subtitle is
89+
extremely long and thus will be displayed in multiple lines.</string>
8890
<!-- Label for a button to change the maximum number of lines [CHAR_LIMIT=32] -->
8991
<string name="menu_max_lines">Max lines = <xliff:g id="max">%d</xliff:g></string>
9092

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

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ public class CollapsingToolbarLayout extends FrameLayout {
196196
private int topInsetApplied = 0;
197197
private boolean forceApplySystemWindowInsetTop;
198198

199-
private int extraMultilineHeight = 0;
199+
private int extraMultilineTitleHeight = 0;
200+
private int extraMultilineSubtitleHeight = 0;
200201
private boolean extraMultilineHeightEnabled;
201202

202203
private int extraHeightForTitles = 0;
@@ -307,7 +308,10 @@ public CollapsingToolbarLayout(
307308
scrimVisibleHeightTrigger =
308309
a.getDimensionPixelSize(R.styleable.CollapsingToolbarLayout_scrimVisibleHeightTrigger, -1);
309310

310-
if (a.hasValue(R.styleable.CollapsingToolbarLayout_maxLines)) {
311+
if (a.hasValue(R.styleable.CollapsingToolbarLayout_titleMaxLines)) {
312+
collapsingTitleHelper.setExpandedMaxLines(
313+
a.getInt(R.styleable.CollapsingToolbarLayout_titleMaxLines, 1));
314+
} else if (a.hasValue(R.styleable.CollapsingToolbarLayout_maxLines)) {
311315
collapsingTitleHelper.setExpandedMaxLines(
312316
a.getInt(R.styleable.CollapsingToolbarLayout_maxLines, 1));
313317
}
@@ -351,6 +355,10 @@ public CollapsingToolbarLayout(
351355
MaterialResources.getColorStateList(
352356
context, a, R.styleable.CollapsingToolbarLayout_collapsedSubtitleTextColor));
353357
}
358+
if (a.hasValue(R.styleable.CollapsingToolbarLayout_subtitleMaxLines)) {
359+
collapsingSubtitleHelper.setExpandedMaxLines(
360+
a.getInt(R.styleable.CollapsingToolbarLayout_subtitleMaxLines, 1));
361+
}
354362
if (a.hasValue(R.styleable.CollapsingToolbarLayout_titlePositionInterpolator)) {
355363
collapsingSubtitleHelper.setPositionInterpolator(
356364
android.view.animation.AnimationUtils.loadInterpolator(
@@ -690,23 +698,42 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
690698
extraHeightForTitles = 0;
691699
}
692700

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;
701+
if (extraMultilineHeightEnabled) {
702+
// Calculates the extra height needed for the multiline title, if needed.
703+
if (collapsingTitleHelper.getExpandedMaxLines() > 1) {
704+
int lineCount = collapsingTitleHelper.getExpandedLineCount();
705+
if (lineCount > 1) {
706+
// Add extra height based on the amount of height beyond the first line of title text.
707+
int expandedTextHeight =
708+
Math.round(collapsingTitleHelper.getExpandedTextFullSingleLineHeight());
709+
extraMultilineTitleHeight = expandedTextHeight * (lineCount - 1);
710+
} else {
711+
extraMultilineTitleHeight = 0;
712+
}
713+
}
714+
// Calculates the extra height needed for the multiline subtitle, if needed.
715+
if (collapsingSubtitleHelper.getExpandedMaxLines() > 1) {
716+
int lineCount = collapsingSubtitleHelper.getExpandedLineCount();
717+
if (lineCount > 1) {
718+
// Add extra height based on the amount of height beyond the first line of subtitle
719+
// text.
720+
int expandedTextHeight =
721+
Math.round(collapsingSubtitleHelper.getExpandedTextFullSingleLineHeight());
722+
extraMultilineSubtitleHeight = expandedTextHeight * (lineCount - 1);
723+
} else {
724+
extraMultilineSubtitleHeight = 0;
725+
}
703726
}
704727
}
705728

706-
if (extraHeightForTitles + extraMultilineHeight > 0) {
729+
if (extraHeightForTitles + extraMultilineTitleHeight + extraMultilineSubtitleHeight > 0) {
707730
heightMeasureSpec =
708731
MeasureSpec.makeMeasureSpec(
709-
originalHeight + extraHeightForTitles + extraMultilineHeight, MeasureSpec.EXACTLY);
732+
originalHeight
733+
+ extraHeightForTitles
734+
+ extraMultilineTitleHeight
735+
+ extraMultilineSubtitleHeight,
736+
MeasureSpec.EXACTLY);
710737
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
711738
}
712739
}
@@ -790,15 +817,16 @@ private void updateTextBounds(
790817
titleBoundsRight,
791818
(int)
792819
(titleBoundsBottom
793-
- collapsingSubtitleHelper.getExpandedTextFullSingleLineHeight()
820+
- (collapsingSubtitleHelper.getExpandedTextFullSingleLineHeight()
821+
+ extraMultilineSubtitleHeight)
794822
- expandedTitleSpacing),
795823
/* alignBaselineAtBottom= */ false);
796824
collapsingSubtitleHelper.setExpandedBounds(
797825
titleBoundsLeft,
798826
(int)
799827
(titleBoundsTop
800828
+ (collapsingTitleHelper.getExpandedTextFullSingleLineHeight()
801-
+ extraMultilineHeight)
829+
+ extraMultilineTitleHeight)
802830
+ expandedTitleSpacing),
803831
titleBoundsRight,
804832
titleBoundsBottom,
@@ -1713,6 +1741,7 @@ public void setExpandedTitleSpacing(int titleSpacing) {
17131741
@RestrictTo(LIBRARY_GROUP)
17141742
public void setMaxLines(int maxLines) {
17151743
collapsingTitleHelper.setExpandedMaxLines(maxLines);
1744+
collapsingSubtitleHelper.setExpandedMaxLines(maxLines);
17161745
}
17171746

17181747
/** Gets the maximum number of lines to display in the expanded state. Experimental Feature. */
@@ -1880,7 +1909,8 @@ public int getScrimVisibleHeightTrigger() {
18801909
// If we have one explicitly set, return it
18811910
return scrimVisibleHeightTrigger
18821911
+ topInsetApplied
1883-
+ extraMultilineHeight
1912+
+ extraMultilineTitleHeight
1913+
+ extraMultilineSubtitleHeight
18841914
+ extraHeightForTitles;
18851915
}
18861916

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@
8989
<public name="scrimVisibleHeightTrigger" type="attr"/>
9090
<public name="titleEnabled" type="attr"/>
9191
<public name="maxLines" type="attr"/>
92+
<public name="titleMaxLines" type="attr"/>
93+
<public name="subtitleMaxLines" type="attr"/>
9294
<public name="titleCentered" type="attr"/>
9395
<public name="subtitleCentered" type="attr"/>
9496
<public name="titlePositionInterpolator" type="attr"/>

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,11 @@
260260
<!-- The expanded title will fade out and translate, and the collapsed title will fade in. -->
261261
<enum name="fade" value="1"/>
262262
</attr>
263-
<!-- The maximum number of lines to display in the expanded state. Experimental Feature. -->
263+
<!-- The maximum number of lines to display the title in the expanded state. Experimental Feature. -->
264264
<attr name="maxLines" format="integer" />
265+
<attr name="titleMaxLines" format="integer"/>
266+
<!-- The maximum number of lines to display the subtitle in the expanded state. Experimental Feature. -->
267+
<attr name="subtitleMaxLines" format="integer" />
265268
<!-- Whether the system window inset top should be applied regardless of
266269
what the layout_height is set to. Experimental Feature. -->
267270
<attr name="forceApplySystemWindowInsetTop" format="boolean" />

0 commit comments

Comments
 (0)