Aray Karjauv
Aray Karjauv

Reputation: 2945

Toolbar in AppBarLayout is not scrolling off if Translucent Navigation is true

UPDATE: here is an example app. I've tested it on my Nexus 6P, Android 6.0.1

I'm using CoordinatorLayout and RecyclerView. If <item name="android:windowTranslucentNavigation">true</item> toolbar is not hiding (but it is scrollable itself). appcompat is v7:23.2.1

<android.support.design.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">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

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

If I add android:fitsSystemWindows="true" to AppBarLayout it works with some issues.

Is there any solution?

System bar overlays toolbar enter image description here

There are top and bottom paddings enter image description here

And CoordinatorLayout (perhaps) scrolls with toolbar enter image description here

Upvotes: 1

Views: 342

Answers (1)

atabek
atabek

Reputation: 89

Seems this is a bug. I found a temporary solution for the problem.

Get rid of all android:fitsSystemWindows in Coordinator Layout and its children, or set them to false. And then add the height of statusbar to your toolbar's height and top padding manually in your activity:

toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
toolbar.getLayoutParams().height = toolbar.getLayoutParams().height + getStatusBarHeight();

The method to get statusbar height:

public int getStatusBarHeight() {
    int height = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        height = getResources().getDimensionPixelSize(resourceId);
    }
    return height;
}

With these changes, toolbar is scrolling properly, and it has proper height.

Upvotes: 2

Related Questions