diff --git a/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java b/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java
index 72163f120..61f779ade 100644
--- a/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java
+++ b/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java
@@ -175,7 +175,10 @@ public class CollapsingToolbarLayout extends FrameLayout {
@Nullable WindowInsetsCompat lastInsets;
private int topInsetApplied = 0;
- private boolean forceApplySystemWindowInsetTop = false;
+ private boolean forceApplySystemWindowInsetTop;
+
+ private int extraMultilineHeight = 0;
+ private boolean extraMultilineHeightEnabled;
public CollapsingToolbarLayout(@NonNull Context context) {
this(context, null);
@@ -290,6 +293,9 @@ public class CollapsingToolbarLayout extends FrameLayout {
forceApplySystemWindowInsetTop =
a.getBoolean(R.styleable.CollapsingToolbarLayout_forceApplySystemWindowInsetTop, false);
+ extraMultilineHeightEnabled =
+ a.getBoolean(R.styleable.CollapsingToolbarLayout_extraMultilineHeightEnabled, false);
+
a.recycle();
setWillNotDraw(false);
@@ -537,11 +543,27 @@ public class CollapsingToolbarLayout extends FrameLayout {
// If we have a top inset and we're set to wrap_content height or force apply,
// we need to make sure we add the top inset to our height, therefore we re-measure
topInsetApplied = topInset;
- heightMeasureSpec =
- MeasureSpec.makeMeasureSpec(getMeasuredHeight() + topInset, MeasureSpec.EXACTLY);
+ int newHeight = getMeasuredHeight() + topInset;
+ heightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
+ if (extraMultilineHeightEnabled && collapsingTextHelper.getMaxLines() > 1) {
+ // Need to update title and bounds in order to calculate line count and text height.
+ updateTitleFromToolbarIfNeeded();
+ updateTextBounds(0, 0, getMeasuredWidth(), getMeasuredHeight(), /* forceRecalculate= */ true);
+
+ int lineCount = collapsingTextHelper.getLineCount();
+ if (lineCount > 1) {
+ // Add extra height based on the amount of height beyond the first line of title text.
+ int expandedTextHeight = Math.round(collapsingTextHelper.getExpandedTextFullHeight());
+ extraMultilineHeight = expandedTextHeight * (lineCount - 1);
+ int newHeight = getMeasuredHeight() + extraMultilineHeight;
+ heightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY);
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+ }
+ }
+
// Set our minimum height to enable proper AppBarLayout collapsing
if (toolbar != null) {
if (toolbarDirectChild == null || toolbarDirectChild == this) {
@@ -576,6 +598,20 @@ public class CollapsingToolbarLayout extends FrameLayout {
getViewOffsetHelper(getChildAt(i)).onViewLayout();
}
+ updateTextBounds(left, top, right, bottom, /* forceRecalculate= */false);
+
+ updateTitleFromToolbarIfNeeded();
+
+ updateScrimVisibility();
+
+ // Apply any view offsets, this should be done at the very end of layout
+ for (int i = 0, z = getChildCount(); i < z; i++) {
+ getViewOffsetHelper(getChildAt(i)).applyOffsets();
+ }
+ }
+
+ private void updateTextBounds(
+ int left, int top, int right, int bottom, boolean forceRecalculate) {
// Update the collapsed bounds by getting its transformed bounds
if (collapsingTitleEnabled && dummyView != null) {
// We only draw the title if the dummy view is being displayed (Toolbar removes
@@ -583,7 +619,7 @@ public class CollapsingToolbarLayout extends FrameLayout {
drawCollapsingTitle =
ViewCompat.isAttachedToWindow(dummyView) && dummyView.getVisibility() == VISIBLE;
- if (drawCollapsingTitle) {
+ if (drawCollapsingTitle || forceRecalculate) {
final boolean isRtl =
ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
@@ -598,23 +634,18 @@ public class CollapsingToolbarLayout extends FrameLayout {
bottom - top - expandedMarginBottom);
// Now recalculate using the new bounds
- collapsingTextHelper.recalculate();
+ collapsingTextHelper.recalculate(forceRecalculate);
}
}
+ }
+ private void updateTitleFromToolbarIfNeeded() {
if (toolbar != null) {
if (collapsingTitleEnabled && TextUtils.isEmpty(collapsingTextHelper.getText())) {
// If we do not currently have a title, try and grab it from the Toolbar
setTitle(getToolbarTitle(toolbar));
}
}
-
- updateScrimVisibility();
-
- // Apply any view offsets, this should be done at the very end of layout
- for (int i = 0, z = getChildCount(); i < z; i++) {
- getViewOffsetHelper(getChildAt(i)).applyOffsets();
- }
}
private void updateCollapsedBounds(boolean isRtl) {
@@ -1346,6 +1377,24 @@ public class CollapsingToolbarLayout extends FrameLayout {
return forceApplySystemWindowInsetTop;
}
+ /**
+ * Sets whether extra height should be added when the title text spans across multiple lines.
+ * Experimental Feature.
+ */
+ @RestrictTo(LIBRARY_GROUP)
+ public void setExtraMultilineHeightEnabled(boolean extraMultilineHeightEnabled) {
+ this.extraMultilineHeightEnabled = extraMultilineHeightEnabled;
+ }
+
+ /**
+ * Gets whether extra height should be added when the title text spans across multiple lines.
+ * Experimental Feature.
+ */
+ @RestrictTo(LIBRARY_GROUP)
+ public boolean isExtraMultilineHeightEnabled() {
+ return extraMultilineHeightEnabled;
+ }
+
/**
* Set the amount of visible height in pixels used to define when to trigger a scrim visibility
* change.
@@ -1374,7 +1423,7 @@ public class CollapsingToolbarLayout extends FrameLayout {
public int getScrimVisibleHeightTrigger() {
if (scrimVisibleHeightTrigger >= 0) {
// If we have one explicitly set, return it
- return scrimVisibleHeightTrigger + topInsetApplied;
+ return scrimVisibleHeightTrigger + topInsetApplied + extraMultilineHeight;
}
// Otherwise we'll use the default computed value
diff --git a/lib/java/com/google/android/material/appbar/res/values/attrs.xml b/lib/java/com/google/android/material/appbar/res/values/attrs.xml
index 6e8d2c2fc..bb9532539 100644
--- a/lib/java/com/google/android/material/appbar/res/values/attrs.xml
+++ b/lib/java/com/google/android/material/appbar/res/values/attrs.xml
@@ -224,6 +224,9 @@
+
+
diff --git a/lib/java/com/google/android/material/internal/CollapsingTextHelper.java b/lib/java/com/google/android/material/internal/CollapsingTextHelper.java
index b4ac470aa..da9fa34b6 100644
--- a/lib/java/com/google/android/material/internal/CollapsingTextHelper.java
+++ b/lib/java/com/google/android/material/internal/CollapsingTextHelper.java
@@ -281,6 +281,12 @@ public final class CollapsingTextHelper {
return -tmpPaint.ascent();
}
+ public float getExpandedTextFullHeight() {
+ getTextPaintExpanded(tmpPaint);
+ // Return expanded height measured from the baseline.
+ return -tmpPaint.ascent() + tmpPaint.descent();
+ }
+
public float getCollapsedTextHeight() {
getTextPaintCollapsed(tmpPaint);
// Return collapsed height measured from the baseline.
@@ -644,11 +650,11 @@ public final class CollapsingTextHelper {
return colorStateList.getDefaultColor();
}
- private void calculateBaseOffsets() {
+ private void calculateBaseOffsets(boolean forceRecalculate) {
final float currentTextSize = this.currentTextSize;
// We then calculate the collapsed text size, using the same logic
- calculateUsingTextSize(collapsedTextSize);
+ calculateUsingTextSize(collapsedTextSize, forceRecalculate);
if (textToDraw != null && textLayout != null) {
textToDrawCollapsed =
TextUtils.ellipsize(textToDraw, textPaint, textLayout.getWidth(), TruncateAt.END);
@@ -689,7 +695,7 @@ public final class CollapsingTextHelper {
break;
}
- calculateUsingTextSize(expandedTextSize);
+ calculateUsingTextSize(expandedTextSize, forceRecalculate);
float expandedTextHeight = textLayout != null ? textLayout.getHeight() : 0;
float measuredWidth = textToDraw != null
@@ -880,8 +886,12 @@ public final class CollapsingTextHelper {
ViewCompat.postInvalidateOnAnimation(view);
}
- @SuppressWarnings("ReferenceEquality") // Matches the Typeface comparison in TextView
private void calculateUsingTextSize(final float textSize) {
+ calculateUsingTextSize(textSize, /* forceRecalculate= */ false);
+ }
+
+ @SuppressWarnings("ReferenceEquality") // Matches the Typeface comparison in TextView
+ private void calculateUsingTextSize(final float textSize, boolean forceRecalculate) {
if (text == null) {
return;
}
@@ -920,14 +930,20 @@ public final class CollapsingTextHelper {
// collapsed text size
float scaledDownWidth = expandedWidth * textSizeRatio;
- // If the scaled down size is larger than the actual collapsed width, we need to
- // cap the available width so that when the expanded text scales down, it matches
- // the collapsed width
- // Otherwise we'll just use the expanded width
+ if (forceRecalculate) {
+ // If we're forcing a recalculate during a measure pass, use the expanded width since the
+ // collapsed width might not be ready yet
+ availableWidth = expandedWidth;
+ } else {
+ // If the scaled down size is larger than the actual collapsed width, we need to
+ // cap the available width so that when the expanded text scales down, it matches
+ // the collapsed width
+ // Otherwise we'll just use the expanded width
- availableWidth = scaledDownWidth > collapsedWidth
- ? min(collapsedWidth / textSizeRatio, expandedWidth)
- : expandedWidth;
+ availableWidth = scaledDownWidth > collapsedWidth
+ ? min(collapsedWidth / textSizeRatio, expandedWidth)
+ : expandedWidth;
+ }
}
if (availableWidth > 0) {
@@ -992,10 +1008,14 @@ public final class CollapsingTextHelper {
}
public void recalculate() {
- if (view.getHeight() > 0 && view.getWidth() > 0) {
+ recalculate(/* forceRecalculate= */ false);
+ }
+
+ public void recalculate(boolean forceRecalculate) {
+ if ((view.getHeight() > 0 && view.getWidth() > 0) || forceRecalculate) {
// If we've already been laid out, calculate everything now otherwise we'll wait
// until a layout
- calculateBaseOffsets();
+ calculateBaseOffsets(forceRecalculate);
calculateCurrentOffsets();
}
}