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)