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

Add marquee (scrolling text) support for MaterialToolbar #4599

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/components/TopAppBar.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ Element | Attr
**`MaterialToolbar` title typography** | `app:titleTextAppearance` | `setTitleTextAppearance` | `?attr/textAppearanceTitleLarge`
**`MaterialToolbar` subtitle typography** | `app:subtitleTextAppearance` | `setSubtitleTextAppearance` | `?attr/textAppearanceTitleMedium`
**`MaterialToolbar` title centering** | `app:titleCentered` | `setTitleCentered` | `false`
**`MaterialToolbar` title marquee** | `app:titleMarqueeEnabled` | `setTitleMarqueeEnabled` | `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
Expand Down
34 changes: 34 additions & 0 deletions lib/java/com/google/android/material/appbar/MaterialToolbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class MaterialToolbar extends Toolbar {
@Nullable private Integer navigationIconTint;
private boolean titleCentered;
private boolean subtitleCentered;
private boolean titleMarqueeEnabled;

@Nullable private ImageView.ScaleType logoScaleType;
@Nullable private Boolean logoAdjustViewBounds;
Expand Down Expand Up @@ -110,6 +111,7 @@ public MaterialToolbar(@NonNull Context context, @Nullable AttributeSet attrs, i

titleCentered = a.getBoolean(R.styleable.MaterialToolbar_titleCentered, false);
subtitleCentered = a.getBoolean(R.styleable.MaterialToolbar_subtitleCentered, false);
titleMarqueeEnabled = a.getBoolean(R.styleable.MaterialToolbar_titleMarqueeEnabled, false);

final int index = a.getInt(R.styleable.MaterialToolbar_logoScaleType, -1);
if (index >= 0 && index < LOGO_SCALE_TYPE_ARRAY.length) {
Expand Down Expand Up @@ -144,6 +146,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto

maybeCenterTitleViews();
updateLogoImageView();
enableMarqueeIfNeeded();
}

private void maybeCenterTitleViews() {
Expand Down Expand Up @@ -228,6 +231,19 @@ private void updateLogoImageView() {
}
}

private void enableMarqueeIfNeeded() {
if (!titleMarqueeEnabled) return;

TextView titleTextView = ToolbarUtils.getTitleTextView(this);
if (titleTextView != null) {
titleTextView.setEllipsize(android.text.TextUtils.TruncateAt.MARQUEE);
titleTextView.setSingleLine(true);
titleTextView.setSelected(true);
titleTextView.setFocusable(true);
titleTextView.setFocusableInTouchMode(true);
}
}

/**
* Returns scale type of logo's ImageView
*
Expand Down Expand Up @@ -347,6 +363,24 @@ public boolean isTitleCentered() {
return titleCentered;
}

/**
* Sets whether the title text corresponding to the {@link #setTitle(int)} method should be
* marquee.
*/
public void setTitleMarqueeEnabled(boolean enabled) {
this.titleMarqueeEnabled = enabled;
requestLayout();
}

/**
* Returns whether the title text corresponding to the {@link #setTitle(int)} method is marquee or not.
*
* @see #setTitleMarqueeEnabled(boolean)
*/
public boolean isTitleMarqueeEnabled() {
return titleMarqueeEnabled;
}

/**
* Sets whether the subtitle text corresponding to the {@link #setSubtitle(int)} method should be
* centered horizontally within the toolbar.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<public name="subtitleMaxLines" type="attr"/>
<public name="titleCentered" type="attr"/>
<public name="subtitleCentered" type="attr"/>
<public name="titleMarqueeEnabled" type="attr"/>
<public name="titlePositionInterpolator" type="attr"/>
<public name="logoAdjustViewBounds" type="attr"/>
<public name="logoScaleType" type="attr"/>
Expand Down