Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CollapsingToolbarLayout] Allow application to Set ellipsis to Title in CollapsingToolBar #2636

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,30 @@
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.material.catalog.topappbar;

import androidx.annotation.LayoutRes;

import io.material.catalog.R;

/** A fragment that displays a large collapsing Top App Bar demo for the Catalog app. */
public class TopAppBarCollapsingLargeMiddleEllipsizeDemoFragment extends BaseTopAppBarCollapsingDemoFragment {

@LayoutRes
protected int getCollapsingToolbarLayoutResId() {
return R.layout.cat_topappbar_collapsing_large_middle_ellipsize_fragment;
}
}
Expand Up @@ -109,6 +109,12 @@ public Fragment createFragment() {
return new TopAppBarCollapsingLargeDemoFragment();
}
},
new Demo(R.string.cat_topappbar_collapsing_large_middle_ellipsize_title) {
@Override
public Fragment createFragment() {
return new TopAppBarCollapsingLargeMiddleEllipsizeDemoFragment();
}
},
new Demo(R.string.cat_topappbar_collapsing_multiline_title) {
@Override
public Fragment createFragment() {
Expand Down
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2017 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbarlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.CollapsingToolbarLayout
style="?attr/collapsingToolbarLayoutLargeStyle"
android:id="@+id/collapsingtoolbarlayout"
android:layout_width="match_parent"
android:layout_height="?attr/collapsingToolbarLayoutLargeSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:titleTextEllipsize="middle">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
style="?attr/catalogToolbarWithCloseButtonStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="0dp"
app:layout_collapseMode="pin"
app:title="Large Collapsing Toolbar Title Ellipsize test"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<include layout="@layout/cat_topappbar_filler_text_view"/>
</androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Expand Up @@ -24,6 +24,7 @@
<string name="cat_topappbar_scrolling_transparent_title" translatable="false">Scrolling Demo (transparent status bar)</string>
<string name="cat_topappbar_collapsing_medium_title" translatable="false">Collapsing Demo (Medium)</string>
<string name="cat_topappbar_collapsing_large_title" translatable="false">Collapsing Demo (Large)</string>
<string name="cat_topappbar_collapsing_large_middle_ellipsize_title" translatable="false">Collapsing Demo (Large Middle Ellipsize)</string>
<string name="cat_topappbar_toolbar_title" translatable="false">Toolbar Demo</string>
<string name="cat_topappbar_collapsing_multiline_title" translatable="false">Collapsing Demo (Multiline title)</string>
<string name="cat_topappbar_action_bar_title" translatable="false">Action Bar Demo</string>
Expand Down
Expand Up @@ -261,6 +261,12 @@ 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)) {
collapsingTextHelper.setTitleTextEllipsize(
a.getInt(R.styleable.CollapsingToolbarLayout_titleTextEllipsize, 2));
}

if (a.hasValue(R.styleable.CollapsingToolbarLayout_expandedTitleTextColor)) {
collapsingTextHelper.setExpandedTextColor(
MaterialResources.getColorStateList(
Expand Down
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,26 @@ public void apply(Typeface font) {
recalculate();
}

public void setTitleTextEllipsize(int ellipsize) {
venugopalraog marked this conversation as resolved.
Show resolved Hide resolved
// Convert to supported TextUtils.TruncateAt values
switch (ellipsize) {
case 0:
titleTextEllipsize = TextUtils.TruncateAt.START;
break;
case 1:
titleTextEllipsize = TextUtils.TruncateAt.MIDDLE;
break;
case 2:
titleTextEllipsize = TextUtils.TruncateAt.END;
break;
case 3:
titleTextEllipsize = TruncateAt.MARQUEE;
venugopalraog marked this conversation as resolved.
Show resolved Hide resolved
default: // fall out
titleTextEllipsize = TextUtils.TruncateAt.END;
break;
}
venugopalraog marked this conversation as resolved.
Show resolved Hide resolved
}

public void setCollapsedTypeface(Typeface typeface) {
if (setCollapsedTypefaceInternal(typeface)) {
recalculate();
Expand Down Expand Up @@ -710,7 +732,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 +1083,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