requestLayout() improperly called
U

5

15

I'm getting this warning spammed in my console. The only info I can find is most people get it when they have fast scroll enabled in a listview. I'm not using any listviews. It says its being improperly called by CollapsingToolbarLayout, AppCompatTextView, Toolbar, and sometimes AppBarLayout. It happens whenever I quickly 'fling' my nested scroll view up and down to collapse and expand the toolbar.

Logs:

06-30 22:18:42.622 19090-19090/? I/art: Not late-enabling -Xcheck:jni (already on)
06-30 22:18:43.153 19090-19090/joebruckner.lastpick.debug D/MovieShuffleActivity: false, true, false
06-30 22:18:43.205 19090-19126/joebruckner.lastpick.debug D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                            [ 06-30 22:18:43.267 19090:19126 D/         ]
                                                                            HostConnection::get() New Host Connection established 0xea95fdb0, tid 19126
06-30 22:18:43.287 19090-19126/joebruckner.lastpick.debug I/OpenGLRenderer: Initialized EGL, version 1.4
06-30 22:18:43.806 19090-19090/joebruckner.lastpick.debug D/MovieShuffleActivity: false, true, true
06-30 22:28:49.264 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{5041bd9 V.ED..... ......ID 0,0-1080,525 #7f0d0074 app:id/collapsingToolbar} during layout: running second layout pass
06-30 22:28:49.264 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.v7.widget.Toolbar{612299e V.E...... ......ID 0,291-1080,438 #7f0d0069 app:id/toolbar} during layout: running second layout pass
06-30 22:28:49.277 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{5041bd9 V.ED..... ........ 0,0-1080,525 #7f0d0074 app:id/collapsingToolbar} during second layout pass: posting in next frame
06-30 22:28:49.456 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{5041bd9 V.ED..... ......ID 0,0-1080,525 #7f0d0074 app:id/collapsingToolbar} during layout: running second layout pass
06-30 22:28:49.456 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.v7.widget.Toolbar{612299e V.E...... ......ID 0,197-1080,344 #7f0d0069 app:id/toolbar} during layout: running second layout pass
06-30 22:28:49.475 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{5041bd9 V.ED..... ........ 0,0-1080,525 #7f0d0074 app:id/collapsingToolbar} during second layout pass: posting in next frame
06-30 22:28:49.696 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{5041bd9 V.ED..... ......ID 0,0-1080,525 #7f0d0074 app:id/collapsingToolbar} during layout: running second layout pass
06-30 22:28:49.696 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.v7.widget.Toolbar{612299e V.E...... ......ID 0,0-1080,147 #7f0d0069 app:id/toolbar} during layout: running second layout pass
06-30 22:28:49.741 19090-19090/joebruckner.lastpick.debug W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{5041bd9 V.ED..... ........ 0,0-1080,525 #7f0d0074 app:id/collapsingToolbar} during second layout pass: posting in next frame

Xml:

<?xml version="1.0" encoding="utf-8"?>
<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/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:titleEnabled="false"
            android:fitsSystemWindows="true">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:contentDescription="@string/backdrop"
                android:scaleType="fitXY"  />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appBar"
        app:layout_behavior="@string/scrolling_view"
        app:behavior_overlapTop="80dp"
        />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_dice_three_48dp"
        app:layout_anchor="@+id/frame"
        app:layout_anchorGravity="bottom|end"
        />
</android.support.design.widget.CoordinatorLayout>
Uniliteral answered 1/7, 2016 at 0:22 Comment(7)
Could you add some logs for reference? Also, are you calling requestLayout manually in any of your code that would be related to the Activity using the collasingtoolbar?Falito
I added logs from flinging the toolbar a couple times. No, I am not calling it anywhere.Uniliteral
Could you post your XML?Falito
I moved my custom textview title out of the collapsing toolbar and I no longer get that as one of the warnings. I'm pretty sure its confined to the contents of AppBarLayoutUniliteral
Have you tried adding an app:layout_collapseMode to your ImageView? I haven't seen a View in a collapsing layout without a collapse mode, so that's the first thing that sticks out.Falito
That's a good point. I'll try that out when I get back.Uniliteral
there is any solutions for this point?String
T
11

my solution is to forward the offset changes only when they differ

       appbar.addOnOffsetChangedListener(object : AppBarLayout.OnOffsetChangedListener {
        var lastOffset = -1
        override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
            if (lastOffset == verticalOffset) return

            lastOffset = verticalOffset
            val percentage = Math.abs(verticalOffset).toFloat() / (appBarLayout?.totalScrollRange?.toFloat() ?: 1f)
            toolbarAnimation.animate(percentage)
        }
    })
Tomlin answered 24/11, 2017 at 10:28 Comment(0)
H
4

Are you setting any AppBarLayout.OnOffsetChangedListener? If so, this could be caused for layout params attribution or any set value of this type in your listener.

If that is your situation, set your layout attributes in the main thread

appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

           //post message back on main thread if you need to modify any views
        }
    });
Hub answered 14/11, 2016 at 0:51 Comment(2)
This is wrong, you are creating Handler every time this method is called, and god, this method is called ALOOOOOOT of times.Vesical
I agree @NikolaDespotoski. Removed the handler creation from the answer.Hub
V
3

This message is serves only as a warning that some view has already requested layout and when another views (in your case Toolbar and CollapsingToolbarLayout) are already requesting layout during the layout phase.

In some cases this lead to performance drawbacks. You should identify:

  1. Do you have some custom View/ViewGroup that calls requestLayout() during scrolling? Do you have other code that calls requestLayout()? Do you have any code that does extensive layout children hierarchy changes, like making view GONE?

  2. When is this happening? Is it only at beginning, just before all views are laid out?

You have the suspected views, that called requestLayout() improperly, you need to find who, when and why is previous making your layout restructure.

Vesical answered 14/11, 2016 at 1:12 Comment(0)
M
1

For kotlin If you set any view's value here it will be flooded with requestLayout process warning. You can try with view visibility gone / visible.

private fun addListener(){
        var isShow = false
        var scrollRange = -1
        tvUserTitle.text = "Hasnine"

        ioCoroutineScope.launch {
            appBar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { barLayout, verticalOffset ->


                if (scrollRange == -1) {
                    scrollRange = barLayout?.totalScrollRange!!
                }
                if (scrollRange + verticalOffset == 0) {
                    //tvTitleHome.title = "Hasnine"
                    //toolbarCollapse.title = "hello world"

                    isShow = true
                    showTitle(isShow)

                } else if (isShow) {
                    //tvTitleHome.title = "" //careful there should a space between double quote otherwise it wont work

                    isShow = false
                    showTitle(isShow)
                }
            })
        }
}

private fun showTitle(show: Boolean){
        if(show){
            tvUserTitle.visibility = View.VISIBLE
        }else{
            tvUserTitle.visibility = View.GONE
        }
    }
Malpighiaceous answered 13/6, 2021 at 12:24 Comment(0)
O
0

Setting title to CollapsingToolbarLayout instead of Toolbar helped to me to solve this problem

collapsingToolbarLayout.setTitle(title);

instead of

toolbar.setTitle(title);
Othilie answered 3/8, 2017 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.