Skip to content

[MaterialToolbar] Error of subtitle alignment #2303

@zsemen

Description

@zsemen

Issue:

The subtitle is aligned to the left if text strings of the title and the subtitle are identical.

Steps:

  1. Use MaterialToolbar for toolbar..
  2. Set both titleCentered and subTitleCentered to true
  3. Set both tile and subTitle to the same vale, for example "aaa".

Example:

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.appbar.MaterialToolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:titleCentered="true"
        app:subtitleCentered="true"
        app:subtitle="aaa"
        app:title="aaa" />

</com.google.android.material.appbar.AppBarLayout>

Root cause is in the code line if (TextUtils.equals(textView.getText(), text)) that results in always returning the TextView of the title and the subTitle is not centered:

private void maybeCenterTitleViews() {
    ...
    TextView subtitleTextView = ToolbarUtils.getSubtitleTextView(this);
    ...
}

public class ToolbarUtils {
...
@Nullable
  public static TextView getSubtitleTextView(@NonNull Toolbar toolbar) {
    return getTextView(toolbar, toolbar.getSubtitle());
  }

  @Nullable
  private static TextView getTextView(@NonNull Toolbar toolbar, CharSequence text) {
    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;
        }
      }
    }
    return null;
  }
}

Error_subtitle_alignment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions