diff --git a/docs/components/TopAppBar.md b/docs/components/TopAppBar.md index f25da2523c4..367d00f2c5d 100644 --- a/docs/components/TopAppBar.md +++ b/docs/components/TopAppBar.md @@ -738,6 +738,33 @@ needs to be set in the menu: ``` +#### 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` 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
`?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` collapsed subtitle typography** | `app:collapsedSubtitleTextAppearance` | `setCollapsedSubtitleTextAppearance` | `?attr/textAppearanceTitleMedium` +**`CollapsingToolbarLayout` expanded subtitle typography** | `app:expandedSubtitleTextAppearance` | `setExpandedSubtitleTextAppearance` | `?attr/textAppearanceTitleLarge` for Medium
`?attr/textAppearanceHeadlineSmall` for Large +**`CollapsingToolbarLayout` collapsed subtitle color** | `android:textColor` (in `app:collapsedSubtitleTextAppearance`) or `app:collapsedSubtitleTextColor` | `setCollapsedSubtitleTextColor` | `?attr/colorOnSurface` +**`CollapsingToolbarLayout` expanded subtitle color** | `android:textColor` (in `app:expandedSubtitleTextAppearance`) or `app:expandedSubtitleTextColor` | `setExpandedSubtitleTextColor` | `?attr/colorOnSurface` +**`CollapsingToolbarLayout` expanded title margins** | `app:expandedTitleMargin*` | `setExpandedTitleMargin*` | `16dp` +**`CollapsingToolbarLayout` padding between expanded title and subtitle** | `app:expandedTitlePadding` | `setExpandedTitlePadding` | `0dp` +**`CollapsingToolbarLayout` title max lines** | `app:maxLines` | `setMaxLines`
`getMaxLines` | `1` +**`CollapsingToolbarLayout` title ellipsize** | `app:titleTextEllipsize` | `setTitleEllipsize`
`getTitleEllipsize` | `end` + +#### Action items attributes For images within collapsing top app bars, set an `android:contentDescription` or use the `setContentDescription` method for the `ImageView`. diff --git a/lib/java/com/google/android/material/appbar/MaterialToolbar.java b/lib/java/com/google/android/material/appbar/MaterialToolbar.java index 7dc96d6d62b..1f816a51b2d 100644 --- a/lib/java/com/google/android/material/appbar/MaterialToolbar.java +++ b/lib/java/com/google/android/material/appbar/MaterialToolbar.java @@ -81,6 +81,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; @@ -108,6 +109,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) { @@ -129,6 +131,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto maybeCenterTitleViews(); updateLogoImageView(); + enableMarqueeIfNeeded(); } private void maybeCenterTitleViews() { @@ -213,6 +216,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 * @@ -332,6 +348,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. 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 a31f4780edf..cc6dbc288fd 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 @@ -97,6 +97,7 @@ +