From f69e3a025b533ed6eb2e20016a5fc31b0e17a9be Mon Sep 17 00:00:00 2001 From: dsn5ft <1420597+dsn5ft@users.noreply.github.com> Date: Thu, 17 Mar 2022 11:08:09 -0400 Subject: [PATCH] [MaterialToolbar] Fixed centering logic when title and subtitle are the same Resolves https://github.com/material-components/material-components-android/issues/2303 PiperOrigin-RevId: 435348290 --- .../material/internal/ToolbarUtils.java | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/java/com/google/android/material/internal/ToolbarUtils.java b/lib/java/com/google/android/material/internal/ToolbarUtils.java index b62497d1b1f..d02d8e37b62 100644 --- a/lib/java/com/google/android/material/internal/ToolbarUtils.java +++ b/lib/java/com/google/android/material/internal/ToolbarUtils.java @@ -16,6 +16,9 @@ package com.google.android.material.internal; +import static java.util.Collections.max; +import static java.util.Collections.min; + import android.graphics.drawable.Drawable; import androidx.appcompat.view.menu.ActionMenuItemView; import androidx.appcompat.widget.ActionMenuView; @@ -30,6 +33,9 @@ import androidx.annotation.Nullable; import androidx.annotation.RestrictTo; import androidx.annotation.RestrictTo.Scope; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; /** * Utility methods for {@link Toolbar}s. @@ -39,32 +45,42 @@ @RestrictTo(Scope.LIBRARY) public class ToolbarUtils { + private static final Comparator VIEW_TOP_COMPARATOR = + new Comparator() { + @Override + public int compare(View view1, View view2) { + return view1.getTop() - view2.getTop(); + } + }; + private ToolbarUtils() { // Private constructor to prevent unwanted construction. } @Nullable public static TextView getTitleTextView(@NonNull Toolbar toolbar) { - return getTextView(toolbar, toolbar.getTitle()); + List textViews = getTextViewsWithText(toolbar, toolbar.getTitle()); + return textViews.isEmpty() ? null : min(textViews, VIEW_TOP_COMPARATOR); } @Nullable public static TextView getSubtitleTextView(@NonNull Toolbar toolbar) { - return getTextView(toolbar, toolbar.getSubtitle()); + List textViews = getTextViewsWithText(toolbar, toolbar.getSubtitle()); + return textViews.isEmpty() ? null : max(textViews, VIEW_TOP_COMPARATOR); } - @Nullable - private static TextView getTextView(@NonNull Toolbar toolbar, CharSequence text) { + private static List getTextViewsWithText(@NonNull Toolbar toolbar, CharSequence text) { + List textViews = new ArrayList<>(); for (int i = 0; i < toolbar.getChildCount(); i++) { View child = toolbar.getChildAt(i); if (child instanceof TextView) { TextView textView = (TextView) child; if (TextUtils.equals(textView.getText(), text)) { - return textView; + textViews.add(textView); } } } - return null; + return textViews; } @Nullable @@ -79,8 +95,7 @@ private static ImageView getImageView(@NonNull Toolbar toolbar, @Nullable Drawab if (child instanceof ImageView) { ImageView imageView = (ImageView) child; if (content != null - && imageView.getDrawable().getConstantState().equals(content.getConstantState()) - ) { + && imageView.getDrawable().getConstantState().equals(content.getConstantState())) { return imageView; } }