@@ -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
0 commit comments