Skip to content

Commit dcfbdff

Browse files
committed
[TopAppBar] Add attribute to center based on whole container
PiperOrigin-RevId: 747496721
1 parent 71bfc93 commit dcfbdff

File tree

6 files changed

+130
-30
lines changed

6 files changed

+130
-30
lines changed

docs/components/TopAppBar.md

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -378,28 +378,31 @@ Element | Attribute | Related method(s)
378378

379379
#### Title attributes
380380

381-
Element | Attribute | Related method(s) | Default value
382-
------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | ------------------------------------------ | -------------
383-
**`MaterialToolbar` title text** | `app:title` | `setTitle`<br>`getTitle` | `null`
384-
**`MaterialToolbar` subtitle text** | `app:subtitle` | `setSubtitle`<br>`getSubtitle` | `null`
385-
**`MaterialToolbar` title color** | `app:titleTextColor` | `setTitleTextColor` | `?attr/colorOnSurface`
386-
**`MaterialToolbar` subtitle color** | `app:subtitleTextColor` | `setSubtitleTextColor` | `?attr/colorOnSurfaceVariant`
387-
**`MaterialToolbar` title typography** | `app:titleTextAppearance` | `setTitleTextAppearance` | `?attr/textAppearanceTitleLarge`
388-
**`MaterialToolbar` subtitle typography** | `app:subtitleTextAppearance` | `setSubtitleTextAppearance` | `?attr/textAppearanceTitleMedium`
389-
**`MaterialToolbar` title centering** | `app:titleCentered` | `setTitleCentered` | `false`
390-
**`MaterialToolbar` subtitle centering** | `app:subtitleCentered` | `setSubtitleCentered` | `false`
391-
**`CollapsingToolbarLayout` collapsed title typography** | `app:collapsedTitleTextAppearance` | `setCollapsedTitleTextAppearance` | `?attr/textAppearanceTitleLarge`
392-
**`CollapsingToolbarLayout` expanded title typography** | `app:expandedTitleTextAppearance` | `setExpandedTitleTextAppearance` | `?attr/textAppearanceHeadlineSmall` for Medium</br>`?attr/textAppearanceHeadlineMedium` for Large
393-
**`CollapsingToolbarLayout` collapsed title color** | `android:textColor` (in `app:collapsedTitleTextAppearance`) or `app:collapsedTitleTextColor` | `setCollapsedTitleTextColor` | `?attr/colorOnSurface`
394-
**`CollapsingToolbarLayout` expanded title color** | `android:textColor` (in `app:expandedTitleTextAppearance`) or `app:expandedTitleTextColor` | `setExpandedTitleTextColor` | `?attr/colorOnSurface`
395-
**`CollapsingToolbarLayout` collapsed subtitle typography** | `app:collapsedSubtitleTextAppearance` | `setCollapsedSubtitleTextAppearance` | `?attr/textAppearanceTitleMedium`
396-
**`CollapsingToolbarLayout` expanded subtitle typography** | `app:expandedSubtitleTextAppearance` | `setExpandedSubtitleTextAppearance` | `?attr/textAppearanceTitleLarge` for Medium</br>`?attr/textAppearanceHeadlineSmall` for Large
397-
**`CollapsingToolbarLayout` collapsed subtitle color** | `android:textColor` (in `app:collapsedSubtitleTextAppearance`) or `app:collapsedSubtitleTextColor` | `setCollapsedSubtitleTextColor` | `?attr/colorOnSurface`
398-
**`CollapsingToolbarLayout` expanded subtitle color** | `android:textColor` (in `app:expandedSubtitleTextAppearance`) or `app:expandedSubtitleTextColor` | `setExpandedSubtitleTextColor` | `?attr/colorOnSurface`
399-
**`CollapsingToolbarLayout` expanded title margins** | `app:expandedTitleMargin*` | `setExpandedTitleMargin*` | `16dp`
400-
**`CollapsingToolbarLayout` padding between expanded title and subtitle** | `app:expandedTitlePadding` | `setExpandedTitlePadding` | `0dp`
401-
**`CollapsingToolbarLayout` title max lines** | `app:maxLines` | `setMaxLines`<br>`getMaxLines` | `1`
402-
**`CollapsingToolbarLayout` title ellipsize** | `app:titleTextEllipsize` | `setTitleEllipsize`<br>`getTitleEllipsize` | `end`
381+
Element | Attribute | Related method(s) | Default value
382+
------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | -------------------------------------------------------- | -------------
383+
**`MaterialToolbar` title text** | `app:title` | `setTitle`<br>`getTitle` | `null`
384+
**`MaterialToolbar` subtitle text** | `app:subtitle` | `setSubtitle`<br>`getSubtitle` | `null`
385+
**`MaterialToolbar` title color** | `app:titleTextColor` | `setTitleTextColor` | `?attr/colorOnSurface`
386+
**`MaterialToolbar` subtitle color** | `app:subtitleTextColor` | `setSubtitleTextColor` | `?attr/colorOnSurfaceVariant`
387+
**`MaterialToolbar` title typography** | `app:titleTextAppearance` | `setTitleTextAppearance` | `?attr/textAppearanceTitleLarge`
388+
**`MaterialToolbar` subtitle typography** | `app:subtitleTextAppearance` | `setSubtitleTextAppearance` | `?attr/textAppearanceTitleMedium`
389+
**`MaterialToolbar` title centering** | `app:titleCentered` | `setTitleCentered` | `false`
390+
**`MaterialToolbar` subtitle centering** | `app:subtitleCentered` | `setSubtitleCentered` | `false`
391+
**`CollapsingToolbarLayout` collapsed title typography** | `app:collapsedTitleTextAppearance` | `setCollapsedTitleTextAppearance` | `?attr/textAppearanceTitleLarge`
392+
**`CollapsingToolbarLayout` expanded title typography** | `app:expandedTitleTextAppearance` | `setExpandedTitleTextAppearance` | `?attr/textAppearanceHeadlineSmall` for Medium</br>`?attr/textAppearanceHeadlineMedium` for Large
393+
**`CollapsingToolbarLayout` collapsed title color** | `android:textColor` (in `app:collapsedTitleTextAppearance`) or `app:collapsedTitleTextColor` | `setCollapsedTitleTextColor` | `?attr/colorOnSurface`
394+
**`CollapsingToolbarLayout` expanded title color** | `android:textColor` (in `app:expandedTitleTextAppearance`) or `app:expandedTitleTextColor` | `setExpandedTitleTextColor` | `?attr/colorOnSurface`
395+
**`CollapsingToolbarLayout` collapsed subtitle typography** | `app:collapsedSubtitleTextAppearance` | `setCollapsedSubtitleTextAppearance` | `?attr/textAppearanceTitleMedium`
396+
**`CollapsingToolbarLayout` expanded subtitle typography** | `app:expandedSubtitleTextAppearance` | `setExpandedSubtitleTextAppearance` | `?attr/textAppearanceTitleLarge` for Medium</br>`?attr/textAppearanceHeadlineSmall` for Large
397+
**`CollapsingToolbarLayout` collapsed subtitle color** | `android:textColor` (in `app:collapsedSubtitleTextAppearance`) or `app:collapsedSubtitleTextColor` | `setCollapsedSubtitleTextColor` | `?attr/colorOnSurface`
398+
**`CollapsingToolbarLayout` expanded subtitle color** | `android:textColor` (in `app:expandedSubtitleTextAppearance`) or `app:expandedSubtitleTextColor` | `setExpandedSubtitleTextColor` | `?attr/colorOnSurface`
399+
**`CollapsingToolbarLayout` expanded title margins** | `app:expandedTitleMargin*` | `setExpandedTitleMargin*` | `16dp`
400+
**`CollapsingToolbarLayout` padding between expanded title and subtitle** | `app:expandedTitlePadding` | `setExpandedTitlePadding` | `0dp`
401+
**`CollapsingToolbarLayout` title max lines** | `app:maxLines` | `setMaxLines`<br>`getMaxLines` | `1`
402+
**`CollapsingToolbarLayout` title ellipsize** | `app:titleTextEllipsize` | `setTitleEllipsize`<br>`getTitleEllipsize` | `end`
403+
**`CollapsingToolbarLayout` collapsed title gravity** | `app:collapsedTitleGravity` | `setCollapsedTitleGravity`<br>`getCollapsedTitleGravity` | `start\|center_vertical`
404+
**`CollapsingToolbarLayout` collapsed title gravity mode** | `app:collapsedTitleGravityMode` | -- | `availableSpace`
405+
**`CollapsingToolbarLayout` expanded title gravity** | `app:expandedTitleGravity` | `setExpandedTitleGravity`<br>`getExpandedTitleGravity` | `start\|bottom`
403406

404407
#### Action items attributes
405408

lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,27 @@ public class CollapsingToolbarLayout extends FrameLayout {
154154
@Retention(RetentionPolicy.SOURCE)
155155
public @interface TitleCollapseMode {}
156156

157+
/**
158+
* The gravity of the collapsed title is based on the entire space of the CollapsedToolbarLayout.
159+
*/
160+
private static final int COLLAPSED_TITLE_GRAVITY_ENTIRE_SPACE = 0;
161+
162+
/**
163+
* The gravity of the collapsed title is based on the remaining space in the
164+
* CollapsedToolbarLayout after accounting for other views such as the menu.
165+
*/
166+
private static final int COLLAPSED_TITLE_GRAVITY_AVAILABLE_SPACE = 1;
167+
168+
/**
169+
* The mode in which to calculate the gravity of the collapsed title.
170+
*
171+
* @hide
172+
*/
173+
@RestrictTo(LIBRARY_GROUP)
174+
@IntDef(value = {COLLAPSED_TITLE_GRAVITY_ENTIRE_SPACE, COLLAPSED_TITLE_GRAVITY_AVAILABLE_SPACE})
175+
@Retention(RetentionPolicy.SOURCE)
176+
public @interface CollapsedTitleGravityMode {}
177+
157178
private boolean refreshToolbar = true;
158179
private int toolbarId;
159180
@Nullable private ViewGroup toolbar;
@@ -172,6 +193,7 @@ public class CollapsingToolbarLayout extends FrameLayout {
172193
@NonNull final ElevationOverlayProvider elevationOverlayProvider;
173194
private boolean collapsingTitleEnabled;
174195
private boolean drawCollapsingTitle;
196+
@CollapsedTitleGravityMode private final int collapsedTitleGravityMode;
175197

176198
@Nullable private Drawable contentScrim;
177199
@Nullable Drawable statusBarScrim;
@@ -235,6 +257,10 @@ public CollapsingToolbarLayout(
235257
a.getInt(
236258
R.styleable.CollapsingToolbarLayout_collapsedTitleGravity,
237259
Gravity.START | Gravity.CENTER_VERTICAL);
260+
collapsedTitleGravityMode =
261+
a.getInt(
262+
R.styleable.CollapsingToolbarLayout_collapsedTitleGravityMode,
263+
COLLAPSED_TITLE_GRAVITY_AVAILABLE_SPACE);
238264

239265
collapsingTitleHelper.setExpandedTextGravity(titleExpandedGravity);
240266
collapsingTitleHelper.setCollapsedTextGravity(titleCollapsedGravity);
@@ -886,6 +912,11 @@ private void updateCollapsedBounds(boolean isRtl) {
886912
final int titleBoundsRight = tmpRect.right - (isRtl ? titleMarginStart : titleMarginEnd);
887913
final int titleBoundsTop = tmpRect.top + maxOffset + titleMarginTop;
888914
final int titleBoundsBottom = tmpRect.bottom + maxOffset - titleMarginBottom;
915+
final int titleBoundsBottomWithSubtitle =
916+
(int) (titleBoundsBottom - collapsingSubtitleHelper.getCollapsedFullSingleLineHeight());
917+
final int subtitleBoundsTop = (int) (titleBoundsTop + collapsingTitleHelper.getCollapsedFullSingleLineHeight());
918+
919+
// Setting the valid collapsed bounds that text can be displayed in
889920
if (TextUtils.isEmpty(collapsingSubtitleHelper.getText())) {
890921
collapsingTitleHelper.setCollapsedBounds(
891922
titleBoundsLeft, titleBoundsTop, titleBoundsRight, titleBoundsBottom);
@@ -894,13 +925,36 @@ private void updateCollapsedBounds(boolean isRtl) {
894925
titleBoundsLeft,
895926
titleBoundsTop,
896927
titleBoundsRight,
897-
(int) (titleBoundsBottom - collapsingSubtitleHelper.getCollapsedFullSingleLineHeight()));
928+
titleBoundsBottomWithSubtitle);
898929
collapsingSubtitleHelper.setCollapsedBounds(
899930
titleBoundsLeft,
900-
(int) (titleBoundsTop + collapsingTitleHelper.getCollapsedFullSingleLineHeight()),
931+
subtitleBoundsTop,
901932
titleBoundsRight,
902933
titleBoundsBottom);
903934
}
935+
936+
// If the collapsed title gravity should be using the whole collapsing toolbar layout instead of
937+
// the dummy layout, we should set the collapsed bounds for offsets.
938+
if (collapsedTitleGravityMode == COLLAPSED_TITLE_GRAVITY_ENTIRE_SPACE) {
939+
DescendantOffsetUtils.getDescendantRect(this, this, tmpRect);
940+
final int validTitleBoundsLeft = tmpRect.left + (isRtl ? titleMarginEnd : titleMarginStart);
941+
final int validTitleBoundsRight = tmpRect.right - (isRtl ? titleMarginStart : titleMarginEnd);
942+
if (TextUtils.isEmpty(collapsingSubtitleHelper.getText())) {
943+
collapsingTitleHelper.setCollapsedBoundsForOffsets(
944+
validTitleBoundsLeft, titleBoundsTop, validTitleBoundsRight, titleBoundsBottom);
945+
} else {
946+
collapsingTitleHelper.setCollapsedBoundsForOffsets(
947+
validTitleBoundsLeft,
948+
titleBoundsTop,
949+
validTitleBoundsRight,
950+
titleBoundsBottomWithSubtitle);
951+
collapsingSubtitleHelper.setCollapsedBoundsForOffsets(
952+
validTitleBoundsLeft,
953+
subtitleBoundsTop,
954+
validTitleBoundsRight,
955+
titleBoundsBottom);
956+
}
957+
}
904958
}
905959

906960
@Nullable

lib/java/com/google/android/material/appbar/res-public/values/public.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<public name="liftOnScrollColor" type="attr"/>
8080
<public name="liftOnScrollTargetViewId" type="attr"/>
8181
<public name="collapsedTitleGravity" type="attr"/>
82+
<public name="collapsedTitleGravityMode" type="attr"/>
8283
<public name="expandedTitleGravity" type="attr"/>
8384
<public name="layout_collapseMode" type="attr"/>
8485
<public name="layout_collapseParallaxMultiplier" type="attr"/>

lib/java/com/google/android/material/appbar/res/values/attrs.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,16 @@
275275
<!-- The interpolator to use when animating the title position from collapsed to expanded and
276276
vice versa. -->
277277
<attr name="titlePositionInterpolator" format="reference"/>
278+
<!-- The mode in which the gravity of the collapsed title is calculated. -->
279+
<attr name="collapsedTitleGravityMode">
280+
<!-- Calculate the gravity based on the entire size of the container, regardless of how
281+
much space is taken by other views in the container. The title will be pushed to the side
282+
if overlapping with another view. -->
283+
<enum name="entireSpace" value="0"/>
284+
<!-- Calculate gravity based on the available space in the container after laying out
285+
other views in the container. -->
286+
<enum name="availableSpace" value="1"/>
287+
</attr>
278288
</declare-styleable>
279289

280290
<declare-styleable name="CollapsingToolbarLayout_Layout">

lib/java/com/google/android/material/appbar/res/values/styles.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<item name="expandedSubtitleTextColor">@macro/m3_comp_app_bar_subtitle_color</item>
2929
<item name="expandedTitleMarginTop">72dp</item>
3030
<item name="expandedTitleMarginBottom">12dp</item>
31+
<item name="collapsedTitleGravityMode">availableSpace</item>
3132
</style>
3233

3334
<style name="Widget.Material3Expressive.CollapsingToolbar.Medium" parent="Widget.Material3Expressive.CollapsingToolbar">

0 commit comments

Comments
 (0)