Skip to content

Commit

Permalink
fix Android title centering bug (#4674)
Browse files Browse the repository at this point in the history
  • Loading branch information
StasDoskalenko authored and guyca committed Feb 3, 2019
1 parent 8cee745 commit 4aa5cd1
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class TitleBar extends Toolbar {

private TitleBarButtonController leftButtonController;
private View component;
private Alignment mAlignment;
private CharSequence mTitle;

public TitleBar(Context context) {
super(context);
Expand All @@ -40,6 +42,10 @@ public TitleBar(Context context) {
public void setTitle(CharSequence title) {
clearComponent();
super.setTitle(title);
if (mTitle != title && mAlignment != null) {
this.setTitleAlignment(mAlignment);
}
mTitle = title;
}

public String getTitle() {
Expand Down Expand Up @@ -72,8 +78,9 @@ public void setTitleTypeface(Typeface typeface) {
}

public void setTitleAlignment(Alignment alignment) {
mAlignment = alignment;
TextView title = findTitleTextView();
if (title == null) return;
if (title == null || title == mTitle) return;
alignTextView(alignment, title);
}

Expand All @@ -94,9 +101,12 @@ public void setSubtitleAlignment(Alignment alignment) {
}

private void alignTextView(Alignment alignment, TextView view) {
int width = view.getResources().getDisplayMetrics().widthPixels;
view.post(() -> {
if (alignment == Alignment.Center) {
view.setX((getWidth() - view.getWidth()) / 2);
view.measure(0, 0);
//noinspection IntegerDivisionInFloatingPointContext
view.setX((width - view.getWidth()) / 2);
} else if (leftButtonController != null) {
view.setX(getContentInsetStartWithNavigation());
} else {
Expand Down

0 comments on commit 4aa5cd1

Please sign in to comment.