Search code examples
androidandroid-activityandroid-statusbar

Statusbar of new activities turns white when using transparent statusbar


Everytime I launch a new activity from my MainActivity the new activity's statusbar turns white. For some reason this problem does not affect the Activity MainActivity which is initially loaded though.

I realize this behavior has been discussed enough on the thread Status bar turns white and does not show content behind it but most of the solutions suggested center around disabling your transparent statusbar and I couldn't get any other solutions to work for me.

This is the layout of one of my activities:

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <FrameLayout
        android:id="@+id/search_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

The attribute android:fitsSystemWindows="true" appears to have no effect here though, since all contents of my activity layout already fit the system window boundaries. It is only that the statusbar is colored completely white.

I rely on my transparent statusbar and thus can not disable it. Is there any other way to prevent the described behavior?


Solution

  • I have now identified two solutions for achieving the desired behavior:

    1. Modifying window flags in the activity's onCreate method:
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    

    This will color the status bar grayish so that the system icons are visible again.

    1. Modifying the background color of the activities root layout to match the desired statusbar color:
    <?xml version="1.0" encoding="utf-8"?>
    
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:id="@+id/root_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:background="@color/colorPrimaryDark">
    
        <FrameLayout
            android:id="@+id/search_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white" />
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    I was able to find the last solution thanks to a friendly comment from Phoenix Wang.