question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[CollapsingToolbarLayout] Wrong height for wrap_content while children have fitSystemWindows set to true

See original GitHub issue

Description: At onMeasure() in CollapsingToolbarLayout, it adds topInset to the measured height, which can be unnecessary and cause a gap to appear at the bottom if children have fitSystemWindow set to true.

ImageView with fitSystemWindow = false

Screenshot_1565883605

ImageView with fitSystemWindow = true

Screenshot_1565883585

Expected behavior: View height should be measured considering fitSystemWindow of children. Screenshot_1565884922

Source code:

<?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:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true">

  <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <com.google.android.material.appbar.CollapsingToolbarLayout
      android:id="@+id/toolbar_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:fitsSystemWindows="true"
      app:contentScrim="?colorPrimary"
      app:layout_scrollFlags="scroll|exitUntilCollapsed"
      app:toolbarId="@+id/toolbar">

      <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:fitsSystemWindows="true"
        android:src="@drawable/hero"/>

      <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </com.google.android.material.appbar.CollapsingToolbarLayout>
  </com.google.android.material.appbar.AppBarLayout>

  <androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_scrolling">

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="16dp"
      android:text="@string/large_text"/>

  </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Android API version: 28

Material Library version: 1.1.0-alpha09

Device: Pixel 2 XL / Pixel 2 Emulator

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:14
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
ogndmancommented, Dec 26, 2019

I’ve solved the issue as follow.

class CustomCollapsingToolbarLayout
@JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : CollapsingToolbarLayout(context, attrs, defStyleAttr) {

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (ViewCompat.getFitsSystemWindows(this)) {
            val mode = MeasureSpec.getMode(heightMeasureSpec)
            val topInset = getTopSystemInset()

            if (mode == MeasureSpec.UNSPECIFIED && topInset > 0) {
                val hasFitsSystemWindowsFlagInChild = (0 until childCount)
                    .map { index -> getChildAt(index) }
                    .any { ViewCompat.getFitsSystemWindows(it) }
                if (hasFitsSystemWindowsFlagInChild) {
                    val heightSpec = MeasureSpec.makeMeasureSpec(measuredHeight - topInset, MeasureSpec.EXACTLY)
                    super.onMeasure(widthMeasureSpec, heightSpec)
                }
            }
        }
    }

    private fun getTopSystemInset(): Int {
        val field = CollapsingToolbarLayout::class.java.getDeclaredField("lastInsets")
            .apply { isAccessible = true }
        val windowsInsetsCompat = field.get(this) as? WindowInsetsCompat
        return windowsInsetsCompat?.systemWindowInsetTop ?: 0
    }
}
0reactions
droplet-jscommented, Jan 7, 2022

i have solve it.

remove “android:fitsSystemWindows=true” from CollapsingToolbarLayout.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AppBarLayout fitSystemtWindows=“true" does not work when ...
I want to build a collapsing Toolbar with views add to CollapsingToolBarLayout dynamically. so i need to set AppBarLayout wrap_content . Then i ......
Read more >
Android CollapsingToolbarLayout Example - DigitalOcean
Android CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child...
Read more >
Handling Scrolls with CoordinatorLayout - CodePath Cliffnotes
When this setting is used, your view will only appear at this minimum height. Only when scrolling reaches to the top will the...
Read more >
Design Support Library (IV): Collapsing Toolbar Layout
Collapsing Toolbar Layout will help us create amazing transition animations when scrolling an activity content and using an image parallax.
Read more >
Set Starting Height Of Collapsingtoolbarlayout - ADocLib
[CollapsingToolbarLayout] Wrong height for wrapcontent while children have fitSystemWindows set to true #520. Open. hiking93 opened this.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found