I am using two different backgrounds for my app depending on which screen is active. In general the status bar is transparent and the background should lay behind it. Depending on the background I need the color of my status bar text to be either light or dark.
So far I used this theme attributes to get the status bar transparent (well its translucent really, but if anyone knows how to get it transparent it would be appreciated)
<item name="android:windowTranslucentStatus">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
I am using Jetpack navigation to navigate and pass arguments to the destination whenever the background should switch like this
mNavController.addOnDestinationChangedListener((controller, destination, arguments) -> {
if (arguments != null) {
setUseSecondaryBackground(arguments.getBoolean("bg_line_style", false));
}
});
This works really good. Now the part that does not work is the switching of the status bar text color:
private void setUseSecondaryBackground(boolean useLineBackground) {
if (useLineBackground) {
//Stuff to switch backgrounds
windowController.setAppearanceLightStatusBars(false); //Works fine
} else {
//Stuff to switch backgrounds
//Does not work!!! If enabled the whole background is shifted downward by the status bar hight - see pictures
windowController.setAppearanceLightStatusBars(true);
}
}
This is the used controller, created in onCreate()
of my MainActivity
windowController = new WindowInsetsControllerCompat(this.getWindow(), this.getWindow().getDecorView());
Here you see that the status bar is green because the background of the mainActivity lays behind the transculent status bar. windowController.setAppearanceLightStatusBars(true);
is commented for this version
Here you see that the status bar is white, the text is white (because it is still the green background) but it got shifted down and replaced by some kind of default white status bar background. Look at where the battery icon is now. windowController.setAppearanceLightStatusBars(true);
Is not commented and was executed somewhere along the road
UPDATE:
Following this I was able to get the desired result on my testing device running Android 10.
On the Emulator Running API 30 I now have one additional Problem:
WindowCompat.setDecorFitsSystemWindows(window, false);
lets us draw behind the status bar but also behind the navigation bar. Now the status bar works as intended but the navigation bar hides part of the UI. I need the navigation bar in its old state and not to be drawn under. Any ideas?
For completeness
compileSdkVersion 31
minSdkVersion 23
targetSdkVersion 31