Search code examples
androidandroid-coordinatorlayoutandroid-appbarlayout

Using windowTranslucentStatus with hidden Toolbar


I'm trying to incorporate windowTranslucentStatus and AppBarLayout with Toolbar flagged with app:layout_scrollFlags="scroll|enterAlways"

Testing simultaneously on APIs 18, 19 and 22 (from the left).

Case study

Problem is on the highest API, where toolbar is partialy visible under the statusbar. I tried many many combinations of styles but this is the closest I got.

In styles I use:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:colorPrimary" tools:targetApi="lollipop">@color/primary</item>
    <item name="android:colorPrimaryDark" tools:targetApi="lollipop">@color/primary_dark</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/primary</item>
    <item name="android:colorAccent" tools:targetApi="lollipop">@color/primary</item>
    <item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
    <item name="android:windowBackground">@color/background</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

Here is my layout xml:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:minHeight="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />


<android.support.design.widget.FloatingActionButton
    android:id="@+id/add_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="@color/green"
    app:layout_anchor="@id/recycler_view"
    app:layout_anchorGravity="bottom|right|end" />

I created sample app, which is available on github

Is there something missing in this puzzle?


Solution

  • Fixed it. It might not be the most beautiful solution, but it wokrs (tested on 19 and 22). I added a negative margin to the coordinator layout and a positive margin to the toolbar (25dp as this is the height of the status bar).

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:layout_marginTop="-25dp">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:minHeight="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                android:layout_marginTop="25dp"/>
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/background"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/add_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:backgroundTint="@color/green"
            app:layout_anchor="@id/recycler_view"
            app:layout_anchorGravity="bottom|right|end" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    enter image description here