-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Closed
Labels
Description
Issue:
The subtitle is aligned to the left if text strings of the title and the subtitle are identical.
Steps:
- Use MaterialToolbar for toolbar..
- Set both titleCentered and subTitleCentered to true
- 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;
}
}
