Skip to content

Commit

Permalink
[Android] Fix title glitch and alignment (#4809)
Browse files Browse the repository at this point in the history
Fix android title glitch and title center alignment issue on layout orientation change
  • Loading branch information
hadimostafapour authored and guyca committed Mar 7, 2019
1 parent b054fca commit 1899601
Showing 1 changed file with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public class TitleBar extends Toolbar {

private TitleBarButtonController leftButtonController;
private View component;
private Alignment mAlignment;
private CharSequence mTitle;
private Alignment titleAlignment;
private Alignment subtitleAlignment;
private Boolean isTitleChanged = false;
private Boolean isSubtitleChanged = false;

public TitleBar(Context context) {
super(context);
Expand All @@ -48,10 +50,13 @@ public void onViewAdded(View child) {
public void setTitle(CharSequence title) {
clearComponent();
super.setTitle(title);
if (mTitle != title && mAlignment != null) {
this.setTitleAlignment(mAlignment);
}
mTitle = title;
isTitleChanged = true;
}

@Override
public void setSubtitle(CharSequence title) {
super.setSubtitle(title);
isSubtitleChanged = true;
}

public String getTitle() {
Expand Down Expand Up @@ -84,10 +89,7 @@ public void setTitleTypeface(Typeface typeface) {
}

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

public void setSubtitleTypeface(Typeface typeface) {
Expand All @@ -101,26 +103,42 @@ public void setSubtitleFontSize(double size) {
}

public void setSubtitleAlignment(Alignment alignment) {
TextView subtitle = findSubtitleTextView();
if (subtitle == null) return;
alignTextView(alignment, subtitle);
subtitleAlignment = alignment;
}

private void alignTextView(Alignment alignment, TextView view) {
Integer direction = view.getParent().getLayoutDirection();
Boolean isRTL = direction == View.LAYOUT_DIRECTION_RTL;
int width = view.getResources().getDisplayMetrics().widthPixels;
view.post(() -> {
if (alignment == Alignment.Center) {
view.measure(0, 0);
//noinspection IntegerDivisionInFloatingPointContext
view.setX((width - view.getWidth()) / 2);
} else if (leftButtonController != null) {
view.setX(isRTL ? (getWidth() - view.getWidth()) - getContentInsetStartWithNavigation() : getContentInsetStartWithNavigation());
} else {
view.setX(isRTL ? (getWidth() - view.getWidth()) - UiUtils.dpToPx(getContext(), DEFAULT_LEFT_MARGIN) : UiUtils.dpToPx(getContext(), DEFAULT_LEFT_MARGIN));

if (alignment == Alignment.Center) {
//noinspection IntegerDivisionInFloatingPointContext
view.setX((getWidth() - view.getWidth()) / 2);
} else if (leftButtonController != null) {
view.setX(isRTL ? (getWidth() - view.getWidth()) - getContentInsetStartWithNavigation() : getContentInsetStartWithNavigation());
} else {
view.setX(isRTL ? (getWidth() - view.getWidth()) - UiUtils.dpToPx(getContext(), DEFAULT_LEFT_MARGIN) : UiUtils.dpToPx(getContext(), DEFAULT_LEFT_MARGIN));
}
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);

if(changed || isTitleChanged) {
TextView title = findTitleTextView();
if (title != null) {
this.alignTextView(titleAlignment, title);
}
});
isTitleChanged = false;
}

if(changed || isSubtitleChanged) {
TextView subtitle = findSubtitleTextView();
if (subtitle != null) {
this.alignTextView(subtitleAlignment, subtitle);
}
isSubtitleChanged = false;
}
}

@Nullable
Expand Down

0 comments on commit 1899601

Please sign in to comment.