Skip to content

Commit

Permalink
[CollapsingToolbarLayout] Allow application to Set ellipsis to Title …
Browse files Browse the repository at this point in the history
…in CollapsingToolBar

Resolves #2636

GIT_ORIGIN_REV_ID=f6c2b07c1eb936ed2de47b7586688a5f6b322e48
PiperOrigin-RevId: 445200467
  • Loading branch information
venugopalraog authored and dsn5ft committed Apr 28, 2022
1 parent bfce84a commit 7b043c3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 18 deletions.
33 changes: 17 additions & 16 deletions docs/components/TopAppBar.md
Expand Up @@ -317,22 +317,23 @@ Element | Attribute | Related method(s)

#### Title attributes

Element | Attribute | Related method(s) | Default value
-------------------------------------------------------- | -------------------------------------------------------------------------------------------- | --------------------------------- | -------------
**`MaterialToolbar` title text** | `app:title` | `setTitle`<br>`getTitle` | `null`
**`MaterialToolbar` subtitle text** | `app:subtitle` | `setSubtitle`<br>`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</br>`?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`<br>`getMaxLines` | `1`
Element | Attribute | Related method(s) | Default value
-------------------------------------------------------- | -------------------------------------------------------------------------------------------- | ------------------------------------------ | -------------
**`MaterialToolbar` title text** | `app:title` | `setTitle`<br>`getTitle` | `null`
**`MaterialToolbar` subtitle text** | `app:subtitle` | `setSubtitle`<br>`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</br>`?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`<br>`getMaxLines` | `1`
**`CollapsingToolbarLayout` title ellipsize** | `app:titleTextEllipsize` | `setTitleEllipsize`<br>`getTitleEllipsize` | `end`

#### Action items attributes

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -87,5 +87,6 @@
<public name="titlePositionInterpolator" type="attr"/>
<public name="logoAdjustViewBounds" type="attr"/>
<public name="logoScaleType" type="attr"/>
<public name="titleTextEllipsize" type="attr"/>
</resources>

Expand Up @@ -152,6 +152,13 @@
<!-- The text appearance of the CollapsingToolbarLayouts title when it is fully
'collapsed' -->
<attr name="collapsedTitleTextAppearance" format="reference"/>
<!-- The text ellipsize of the collapsedToolbarLayout title -->
<attr name="titleTextEllipsize" format="enum">
<enum name="start" value="0" />
<enum name="middle" value="1" />
<enum name="end" value="2" />
<enum name="marquee" value="3" />
</attr>
<!-- The text color of the CollapsingToolbarLayout's title when it is fully
'expanded' -->
<attr name="expandedTitleTextColor" format="color|reference"/>
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7b043c3

Please sign in to comment.