4

I've noticed two small kinks with the collapsing toolbar I have implemented. The first thing i noticed is that it applies an inset between the toolbar and the recyclerview/nested scrollview below it creating a noticeable gap as shown in picture A

Picture A Inset Gap

The second kink I notice is when I scroll down the list and the toolbar collapses. If I click on one of the recycler view items and navigate to the next fragment, then click the back button and navigate back, the toolbar will be collapsed, however the image is visible and takes a second or two to animate and become invisible again as shown in picture B.

Toolbar Image visible for 1 second onResume from other fragment

My XML LAYOUT

<?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:id="@+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/transparent"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:contentScrim="@color/logs_header"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:statusBarScrim="@color/logs_header">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/child"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax">

            <ImageView
                android:id="@+id/headerImage"
                android:layout_width="match_parent"
                android:layout_height="@dimen/checkin_header_image"
                android:scaleType="fitXY"
                app:layout_collapseMode="parallax"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_logs_header" />

            <ImageView
                android:id="@+id/brain"
                android:layout_width="@dimen/layout_brain_size"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/_32sdp"
                android:adjustViewBounds="true"
                app:layout_collapseMode="parallax"
                 app:layout_constraintBottom_toBottomOf="@+id/headerImage"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/headerImage"
                app:srcCompat="@drawable/brain_workout_logs" />

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/heading"
                style="@style/heading.style"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/heading_margin_lr"
                android:layout_marginTop="@dimen/heading_margin_top"
                android:layout_marginEnd="@dimen/heading_margin_lr"
                android:text="@string/logs_title"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/headerImage" />

            <View
                android:id="@+id/dividerHeading"
                android:layout_width="match_parent"
                android:layout_height="@dimen/divider_height"
                android:layout_marginStart="@dimen/divider_heading_lr"
                android:layout_marginTop="@dimen/_12sdp"
                android:layout_marginEnd="@dimen/divider_heading_lr"
                android:background="?android:attr/dividerVertical"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/heading" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbarContainer"
            android:layout_width="match_parent"
            android:layout_height="@dimen/toolbar_height"
            android:background="@color/transparent"
            app:contentInsetStart="0dp"
            app:layout_collapseMode="pin">

            <include
                android:id="@+id/toolbarLayout"
                layout="@layout/toolbar_logs" />
        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:id="@+id/childContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:overScrollMode="never"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvLogs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_6sdp"
            android:layout_marginEnd="@dimen/_6sdp"
            android:nestedScrollingEnabled="false"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Could anyone help me root cause and fix both of these small bugs?

AndroidDev123
  • 280
  • 3
  • 24

1 Answers1

0

For the space issue you have to remove the android:fitsSystemWindows="true" attribute from your root CoordinatorLayout. If it's true, then it adjusts the padding of this view to leave space for the system windows (eg: for the status bar). According to the documentation

Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows.

MariosP
  • 8,300
  • 1
  • 9
  • 30
  • But I want it to draw behind the status bar, having fitsSystemWindow = true enables this – AndroidDev123 Feb 26 '21 at 14:27
  • What about removing this property only from the ConstraintLayout (android:id="@+id/child") and keep this property on the root? – MariosP Feb 26 '21 at 15:04
  • That partially works in that it removes the spacing but it no longer draws behind the status bar, its just white now – AndroidDev123 Feb 26 '21 at 18:13
  • To draw behind the status bar you can set true for API 19 and above in your app theme but there is also a great SO thread which doing the same thing: https://stackoverflow.com/questions/27856603/lollipop-draw-behind-statusbar-with-its-color-set-to-transparent – MariosP Feb 27 '21 at 08:50