I create simple ScrollingActivity (which created by new Android template) it's working fine while scrolling
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ScrollingActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/Theme.TestApplication.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@+id/toolbar">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/Theme.TestApplication.PopupOverlay" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_scrolling" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/fab_margin"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"
app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
However, after I use setAppearanceLightStatusBars in addOnOffsetChangedListener, the CollapsingToolbarLayout not work well anymore
binding.appBar.addOnOffsetChangedListener { appbarLayout, verticalOffset: Int ->
val seekPosition = -verticalOffset / appbarLayout.totalScrollRange.toFloat()
val isLightStatusBar = seekPosition > 0.5
if (isLightStatusBar != WindowCompat.getInsetsController(window, window.decorView).isAppearanceLightStatusBars) {
WindowCompat.getInsetsController(
window, window.decorView
).isAppearanceLightStatusBars = isLightStatusBar
}
}
This problem is only happen on Android 26 (or maybe lower), it's not happens with newer Android version.
Beside that, the issue will not happen if I use SYSTEM_UI_FLAG_LIGHT_STATUS_BAR to set light statusbar (but SYSTEM_UI_FLAG_LIGHT_STATUS_BAR deprecated)
This is just a workaround as I couldn't find any solution
After checking the code, currently
WindowInsetsControllerCompat#isAppearanceLightStatusBars = truewill do thoes things on Android < 30
And I figure out that the scroll problem in collapsing toolbar only happens when we combine the 1st flag to 2nd or 3rd flag.
Also, its weird that problem is not happens on Android 29, but it happens on 26,27,28
Finally, I end up with using only
SYSTEM_UI_FLAG_LIGHT_STATUS_BARfor API < 30 and keep usingWindowInsetsControllerCompatfor API >= 30