Skip to content

Commit e3763f0

Browse files
authored
Merge pull request #1487 from nvllz/autohide-fix
Fixes to actionbar/TTS misplacement
2 parents 68122b9 + a942936 commit e3763f0

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

app/src/main/java/fr/gaulupeau/apps/Poche/ui/ReadArticleActivity.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.view.View;
1515
import android.view.View.OnClickListener;
1616
import android.view.ViewGroup;
17+
import android.view.ViewTreeObserver;
1718
import android.view.Window;
1819
import android.view.WindowManager;
1920
import android.webkit.ConsoleMessage;
@@ -298,6 +299,7 @@ public void onStop() {
298299

299300
@Override
300301
protected void onDestroy() {
302+
removeTtsContainerHeightListener();
301303
EventHelper.unregister(this);
302304

303305
super.onDestroy();
@@ -1293,12 +1295,14 @@ public void toggleTTS(boolean autoPlay) {
12931295

12941296
getSupportFragmentManager()
12951297
.beginTransaction()
1296-
.add(R.id.viewMain, ttsFragment, TAG_TTS_FRAGMENT)
1298+
.add(R.id.tts_container, ttsFragment, TAG_TTS_FRAGMENT)
12971299
.commit();
12981300

12991301
settings.setTtsVisible(true);
13001302

13011303
initTtsForArticle();
1304+
1305+
setupTtsContainerHeightListener();
13021306
} else {
13031307
getSupportFragmentManager()
13041308
.beginTransaction()
@@ -1308,11 +1312,64 @@ public void toggleTTS(boolean autoPlay) {
13081312
ttsFragment = null;
13091313

13101314
settings.setTtsVisible(false);
1315+
1316+
adjustScrollViewPaddingForTts(0);
1317+
1318+
removeTtsContainerHeightListener();
13111319
}
13121320

13131321
invalidateOptionsMenu();
13141322
}
13151323

1324+
private ViewTreeObserver.OnGlobalLayoutListener ttsContainerLayoutListener;
1325+
1326+
private void setupTtsContainerHeightListener() {
1327+
FrameLayout ttsContainer = findViewById(R.id.tts_container);
1328+
if (ttsContainer == null) return;
1329+
1330+
removeTtsContainerHeightListener();
1331+
1332+
ttsContainerLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
1333+
private int lastHeight = -1;
1334+
1335+
@Override
1336+
public void onGlobalLayout() {
1337+
FrameLayout container = findViewById(R.id.tts_container);
1338+
if (container != null) {
1339+
int currentHeight = container.getHeight();
1340+
if (currentHeight != lastHeight && currentHeight > 0) {
1341+
lastHeight = currentHeight;
1342+
adjustScrollViewPaddingForTts(currentHeight);
1343+
}
1344+
}
1345+
}
1346+
};
1347+
1348+
ttsContainer.getViewTreeObserver().addOnGlobalLayoutListener(ttsContainerLayoutListener);
1349+
}
1350+
1351+
private void removeTtsContainerHeightListener() {
1352+
if (ttsContainerLayoutListener != null) {
1353+
FrameLayout ttsContainer = findViewById(R.id.tts_container);
1354+
if (ttsContainer != null) {
1355+
ttsContainer.getViewTreeObserver().removeOnGlobalLayoutListener(ttsContainerLayoutListener);
1356+
ttsContainer.getViewTreeObserver().removeOnGlobalLayoutListener(ttsContainerLayoutListener);
1357+
}
1358+
ttsContainerLayoutListener = null;
1359+
}
1360+
}
1361+
1362+
private void adjustScrollViewPaddingForTts(int ttsHeight) {
1363+
if (scrollView != null) {
1364+
scrollView.setPadding(
1365+
scrollView.getPaddingLeft(),
1366+
scrollView.getPaddingTop(),
1367+
scrollView.getPaddingRight(),
1368+
ttsHeight
1369+
);
1370+
}
1371+
}
1372+
13161373
private void initTtsForArticle() {
13171374
if (ttsFragment != null) {
13181375
ttsFragment.initForArticle(article);

app/src/main/res/layout/article.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
android:id="@+id/app_bar"
1313
android:layout_width="match_parent"
1414
android:layout_height="wrap_content"
15-
android:fitsSystemWindows="true">
15+
android:fitsSystemWindows="true"
16+
app:liftOnScroll="true">
1617

1718
<androidx.appcompat.widget.Toolbar
1819
android:id="@+id/toolbar"
1920
android:layout_width="match_parent"
2021
android:layout_height="?attr/actionBarSize"
2122
android:theme="?attr/actionBarTheme"
22-
app:layout_scrollFlags="scroll|enterAlways|snap" />
23+
app:layout_scrollFlags="scroll|enterAlways|snap|enterAlwaysCollapsed" />
2324

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

@@ -106,4 +107,12 @@
106107

107108
</androidx.core.widget.NestedScrollView>
108109

110+
<FrameLayout
111+
android:id="@+id/tts_container"
112+
android:layout_width="match_parent"
113+
android:layout_height="wrap_content"
114+
android:layout_gravity="bottom"
115+
android:background="?android:attr/windowBackground"
116+
android:elevation="8dp"/>
117+
109118
</androidx.coordinatorlayout.widget.CoordinatorLayout>

0 commit comments

Comments
 (0)