diff --git a/docs/components/TopAppBar.md b/docs/components/TopAppBar.md index f1bfd5260d2..1f90fe57082 100644 --- a/docs/components/TopAppBar.md +++ b/docs/components/TopAppBar.md @@ -317,22 +317,23 @@ Element | Attribute | Related method(s) #### Title attributes -Element | Attribute | Related method(s) | Default value --------------------------------------------------------- | -------------------------------------------------------------------------------------------- | --------------------------------- | ------------- -**`MaterialToolbar` title text** | `app:title` | `setTitle`
`getTitle` | `null` -**`MaterialToolbar` subtitle text** | `app:subtitle` | `setSubtitle`
`getSubtitle` | `null` -**`MaterialToolbar` title color** | `app:titleTextColor` | `setTitleTextColor` | `?attr/colorOnSurface` -**`MaterialToolbar` subtitle color** | `app:subtitleTextColor` | `setSubtitleTextColor` | `?attr/colorOnSurfaceVariant` -**`MaterialToolbar` title typography** | `app:titleTextAppearance` | `setTitleTextAppearance` | `?attr/textAppearanceTitleLarge` -**`MaterialToolbar` subtitle typography** | `app:subtitleTextAppearance` | `setSubtitleTextAppearance` | `?attr/textAppearanceTitleMedium` -**`MaterialToolbar` title centering** | `app:titleCentered` | `setTitleCentered` | `false` -**`MaterialToolbar` subtitle centering** | `app:subtitleCentered` | `setSubtitleCentered` | `false` -**`CollapsingToolbarLayout` collapsed title typography** | `app:collapsedTitleTextAppearance` | `setCollapsedTitleTextAppearance` | `?attr/textAppearanceTitleLarge` -**`CollapsingToolbarLayout` expanded title typography** | `app:expandedTitleTextAppearance` | `setExpandedTitleTextAppearance` | `?attr/textAppearanceHeadlineSmall` for Medium
`?attr/textAppearanceHeadlineMedium` for Large -**`CollapsingToolbarLayout` collapsed title color** | `android:textColor` (in `app:collapsedTitleTextAppearance`) or `app:collapsedTitleTextColor` | `setCollapsedTitleTextColor` | `?attr/colorOnSurface` -**`CollapsingToolbarLayout` expanded title color** | `android:textColor` (in `app:expandedTitleTextAppearance`) or `app:expandedTitleTextColor` | `setExpandedTitleTextColor` | `?attr/colorOnSurface` -**`CollapsingToolbarLayout` expanded title margins** | `app:expandedTitleMargin*` | `setExpandedTitleMargin*` | `16dp` -**`CollapsingToolbarLayout` title max lines** | `app:maxLines` | `setMaxLines`
`getMaxLines` | `1` +Element | Attribute | Related method(s) | Default value +-------------------------------------------------------- | -------------------------------------------------------------------------------------------- | ------------------------------------------ | ------------- +**`MaterialToolbar` title text** | `app:title` | `setTitle`
`getTitle` | `null` +**`MaterialToolbar` subtitle text** | `app:subtitle` | `setSubtitle`
`getSubtitle` | `null` +**`MaterialToolbar` title color** | `app:titleTextColor` | `setTitleTextColor` | `?attr/colorOnSurface` +**`MaterialToolbar` subtitle color** | `app:subtitleTextColor` | `setSubtitleTextColor` | `?attr/colorOnSurfaceVariant` +**`MaterialToolbar` title typography** | `app:titleTextAppearance` | `setTitleTextAppearance` | `?attr/textAppearanceTitleLarge` +**`MaterialToolbar` subtitle typography** | `app:subtitleTextAppearance` | `setSubtitleTextAppearance` | `?attr/textAppearanceTitleMedium` +**`MaterialToolbar` title centering** | `app:titleCentered` | `setTitleCentered` | `false` +**`MaterialToolbar` subtitle centering** | `app:subtitleCentered` | `setSubtitleCentered` | `false` +**`CollapsingToolbarLayout` collapsed title typography** | `app:collapsedTitleTextAppearance` | `setCollapsedTitleTextAppearance` | `?attr/textAppearanceTitleLarge` +**`CollapsingToolbarLayout` expanded title typography** | `app:expandedTitleTextAppearance` | `setExpandedTitleTextAppearance` | `?attr/textAppearanceHeadlineSmall` for Medium
`?attr/textAppearanceHeadlineMedium` for Large +**`CollapsingToolbarLayout` collapsed title color** | `android:textColor` (in `app:collapsedTitleTextAppearance`) or `app:collapsedTitleTextColor` | `setCollapsedTitleTextColor` | `?attr/colorOnSurface` +**`CollapsingToolbarLayout` expanded title color** | `android:textColor` (in `app:expandedTitleTextAppearance`) or `app:expandedTitleTextColor` | `setExpandedTitleTextColor` | `?attr/colorOnSurface` +**`CollapsingToolbarLayout` expanded title margins** | `app:expandedTitleMargin*` | `setExpandedTitleMargin*` | `16dp` +**`CollapsingToolbarLayout` title max lines** | `app:maxLines` | `setMaxLines`
`getMaxLines` | `1` +**`CollapsingToolbarLayout` title ellipsize** | `app:titleTextEllipsize` | `setTitleEllipsize`
`getTitleEllipsize` | `end` #### Action items attributes diff --git a/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java b/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java index 388b990cd98..679565f64c7 100644 --- a/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java +++ b/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java @@ -36,6 +36,7 @@ import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; import android.text.TextUtils; +import android.text.TextUtils.TruncateAt; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; @@ -261,6 +262,13 @@ public CollapsingToolbarLayout(@NonNull Context context, @Nullable AttributeSet a.getResourceId(R.styleable.CollapsingToolbarLayout_collapsedTitleTextAppearance, 0)); } + // Now overlay any custom text Ellipsize + if (a.hasValue(R.styleable.CollapsingToolbarLayout_titleTextEllipsize)) { + setTitleEllipsize( + convertEllipsizeToTruncateAt( + a.getInt(R.styleable.CollapsingToolbarLayout_titleTextEllipsize, -1))); + } + if (a.hasValue(R.styleable.CollapsingToolbarLayout_expandedTitleTextColor)) { collapsingTextHelper.setExpandedTextColor( MaterialResources.getColorStateList( @@ -814,6 +822,40 @@ public boolean isTitleEnabled() { return collapsingTitleEnabled; } + + /** + * Set ellipsizing on the title text. + * + * @param ellipsize type of ellipsis behavior + * @attr ref R.styleable#CollapsingToolbarLayout_titleTextEllipsize + */ + public void setTitleEllipsize(@NonNull TruncateAt ellipsize) { + collapsingTextHelper.setTitleTextEllipsize(ellipsize); + } + + /** + * Get ellipsizing currently applied on the title text. + */ + @NonNull + public TruncateAt getTitleTextEllipsize() { + return collapsingTextHelper.getTitleTextEllipsize(); + } + + // Convert to supported TruncateAt values + private TruncateAt convertEllipsizeToTruncateAt(int ellipsize) { + switch (ellipsize) { + case 0: + return TruncateAt.START; + case 1: + return TruncateAt.MIDDLE; + case 3: + return TruncateAt.MARQUEE; + case 2: + default: + return TruncateAt.END; + } + } + /** * Set whether the content scrim and/or status bar scrim should be shown or not. Any change in the * vertical scroll may overwrite this value. Any visibility change will be animated if this view diff --git a/lib/java/com/google/android/material/appbar/res-public/values/public.xml b/lib/java/com/google/android/material/appbar/res-public/values/public.xml index c302eb3d807..3731bb2795d 100644 --- a/lib/java/com/google/android/material/appbar/res-public/values/public.xml +++ b/lib/java/com/google/android/material/appbar/res-public/values/public.xml @@ -87,5 +87,6 @@ + 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 11b70d395d7..5a81d74a6dd 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 @@ -152,6 +152,13 @@ + + + + + + + diff --git a/lib/java/com/google/android/material/internal/CollapsingTextHelper.java b/lib/java/com/google/android/material/internal/CollapsingTextHelper.java index ce634437ee9..e7c17704648 100644 --- a/lib/java/com/google/android/material/internal/CollapsingTextHelper.java +++ b/lib/java/com/google/android/material/internal/CollapsingTextHelper.java @@ -126,6 +126,8 @@ public final class CollapsingTextHelper { private CancelableFontCallback expandedFontCallback; private CancelableFontCallback collapsedFontCallback; + private TruncateAt titleTextEllipsize = TruncateAt.END; + @Nullable private CharSequence text; @Nullable private CharSequence textToDraw; private boolean isRtl; @@ -447,6 +449,16 @@ public void apply(Typeface font) { recalculate(); } + public void setTitleTextEllipsize(@NonNull TruncateAt ellipsize) { + titleTextEllipsize = ellipsize; + recalculate(); + } + + @NonNull + public TruncateAt getTitleTextEllipsize() { + return titleTextEllipsize; + } + public void setCollapsedTypeface(Typeface typeface) { if (setCollapsedTypefaceInternal(typeface)) { recalculate(); @@ -710,7 +722,7 @@ private void calculateBaseOffsets(boolean forceRecalculate) { calculateUsingTextSize(/* fraction= */ 1, forceRecalculate); if (textToDraw != null && textLayout != null) { textToDrawCollapsed = - TextUtils.ellipsize(textToDraw, textPaint, textLayout.getWidth(), TruncateAt.END); + TextUtils.ellipsize(textToDraw, textPaint, textLayout.getWidth(), titleTextEllipsize); } if (textToDrawCollapsed != null) { collapsedTextWidth = measureTextWidth(textPaint, textToDrawCollapsed); @@ -1061,7 +1073,7 @@ private StaticLayout createStaticLayout(int maxLines, float availableWidth, bool Alignment textAlignment = maxLines == 1 ? ALIGN_NORMAL : getMultilineTextLayoutAlignment(); textLayout = StaticLayoutBuilderCompat.obtain(text, textPaint, (int) availableWidth) - .setEllipsize(TruncateAt.END) + .setEllipsize(titleTextEllipsize) .setIsRtl(isRtl) .setAlignment(textAlignment) .setIncludePad(false)