From 0df1bcce318346039177216fc3621c20cde17bc6 Mon Sep 17 00:00:00 2001 From: sk Date: Mon, 16 Jan 2023 16:46:09 +0100 Subject: [PATCH] fix new posts centered layout --- .../android/fragments/HomeTabFragment.java | 42 ++++++++++--------- mastodon/src/main/res/layout/home_toolbar.xml | 7 +++- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTabFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTabFragment.java index 7c3c45d15..e5b35f2ba 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTabFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTabFragment.java @@ -208,26 +208,6 @@ public class HomeTabFragment extends MastodonToolbarFragment implements Scrollab error.showToast(getContext()); } }).exec(accountID); - - ViewTreeObserver vto = view.getViewTreeObserver(); - if (vto.isAlive()) { - vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { - @Override - public void onGlobalLayout() { - view.getViewTreeObserver().removeOnGlobalLayoutListener(this); - HomeTabFragment.this.onGlobalLayout(); - } - }); - } - } - - private void onGlobalLayout() { - switcher.setPivotX(V.dp(28)); // padding + half of icon - switcher.setPivotY(switcher.getHeight() / 2f); - // toolbar frame goes from screen edge to beginning of right-aligned option buttons. - // centering button by applying the same space on the left - int padding = getToolbar().getWidth() - toolbarFrame.getWidth(); - ((FrameLayout) toolbarShowNewPostsBtn.getParent()).setPaddingRelative(padding, 0, 0, 0); } public void updateToolbarLogo(){ @@ -269,6 +249,28 @@ public class HomeTabFragment extends MastodonToolbarFragment implements Scrollab } showNewPostsButton(); + + ViewTreeObserver vto = toolbar.getViewTreeObserver(); + if (vto.isAlive()) { + vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + int toolbarWidth = getToolbar().getWidth(); + if (toolbarWidth == 0) return; + toolbar.getViewTreeObserver().removeOnGlobalLayoutListener(this); + + int toolbarFrameWidth = toolbarFrame.getWidth(); + int padding = toolbarWidth - toolbarFrameWidth; + // toolbar frame goes from screen edge to beginning of right-aligned option buttons. + // centering button by applying the same space on the left + ((FrameLayout) toolbarShowNewPostsBtn.getParent()).setPaddingRelative(padding, 0, 0, 0); + toolbarShowNewPostsBtn.setMaxWidth(toolbarWidth - padding * 2); + + switcher.setPivotX(V.dp(28)); // padding + half of icon + switcher.setPivotY(switcher.getHeight() / 2f); + } + }); + } } @Override diff --git a/mastodon/src/main/res/layout/home_toolbar.xml b/mastodon/src/main/res/layout/home_toolbar.xml index 9fe9fb19f..abdc1049e 100644 --- a/mastodon/src/main/res/layout/home_toolbar.xml +++ b/mastodon/src/main/res/layout/home_toolbar.xml @@ -44,7 +44,12 @@