How to reset the Toolbar position controlled by the CoordinatorLayout?

coordinatorlayout recyclerview scroll
android coordinatorlayout scroll programmatically
android coordinatorlayout scrollview
android coordinatorlayout behavior
coordinatorlayout androidx
appbar_scrolling_view_behavior not working
how to disable scrolling in collapsing toolbar layout android
collapsing toolbar with recyclerview android example

The app I'm working on consists of a Navigation Drawer which is implemented in an Activity. The activity layout is as follows:

<FrameLayout 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.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include
            android:id="@+id/appbar"
            layout="@layout/appbar" />

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

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header_drawer"
        app:menu="@menu/menu_nav">
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

</FrameLayout>

This is a very common pattern, only thing that changes frequently is the Fragment inside the container layout.

If any of the Fragment has a scrolling element, upon scrolling, the CoordinatorLayout will happily make position translations, including the Toolbar/AppBarLayout.

The real problem here is, when the Fragment gets replaced, the position for the Toolbar remains the same, i.e., if the Toolbar is hidden, it will stay so which isn't intended.

Result is this:

This:

Gets stuck:

How can one reset the Toolbar position for this case?

EDIT: A bug is probable, the AppBarLayout offset change listener gets called only when relaunching the app (press back button and open the app), and stops getting called again after an intense fling.

To reset the scroll state, just get the AppBarLayout.Behavior object

CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator);
AppBarLayout appbar = (AppBarLayout) findViewById(R.id.appbar);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();       

and call onNestedPreScroll method manually:

int[] consumed = new int[2];
behavior.onNestedPreScroll(coordinator, appbar, null, 0, -1000, consumed);

If you would like to reset smoothly with an animation, you can try calling onNestedFling instead:

behavior.onNestedFling(coordinator, appbar, null, 0, -1000, true);

How to reset the Toolbar position controlled by the - android, If any of the Fragment has a scrolling element, upon scrolling, the CoordinatorLayout will happily make position translations, including the Toolbar/ AppBarLayout� You can see that the CollapsingToolbarLayout is placed inside the AppBarLayout and it contains an ImageView and Toolbar widget. The FloatingActionButton control is place at the end of the CoordinatorLayout but with app:layout_anchor= “@id/app_bar_layout”, it will be attached to the bottom right of the AppBarLayout

First get a AppBarLayout refrence in you MainActivity, then in the pause state of the fragment that is being replaced, use the method below to expand toolbar :

MainActivity.appbar.setExpanded(true,true);

And or to close the toolbar :

MainActivity.appbar.setExpanded(false,true);

The second parameter is used to scroll the toolbar smoothly.

How to reset the Toolbar position controlled by the , CoordinatorLayout android:id="@ id/coordinator" scrolling, the CoordinatorLayout will happily make position translations, including the Toolbar/ AppBarLayout. c'est un motif très courant, la seule chose qui change fréquemment est le Fragment à l'intérieur de la disposition du conteneur. si un Fragment a un élément de défilement, lors du défilement, le CoordinatorLayout fera volontiers des translations de position, y compris la barre d'outils/AppBarLayout.

Update your support lib to v23 then you can use:

appBarLayout.setExpanded(true/false);

public void setExpanded (boolean expanded)

Sets whether this AppBarLayout is expanded or not, animating if it has already been laid out.

As with AppBarLayout's scrolling, this method relies on this layout being a direct child of a CoordinatorLayout.

expanded true if the layout should be fully expanded, false if it should be fully collapsed

Handling Scrolls with CoordinatorLayout, Expanding or contracting the Toolbar or header space to make room for the When a Snackbar is rendered, it normally appears at the bottom of the visible screen. the pager are bubbled up and can be managed by the CoordinatorLayout . So basically CoordinatorLayout provide control over components and how they behave in the presence of each other. In this post, we will understand how to create different types of CoordinatorLayout. With CoordinatorLayout we will design the following screens. 1. Android CoordinatorLayout with Toolbar and Floating Action Button

@razzledazzle The AppBarLayout stores onOffsetChangedListeners as WeakReferences, which means they are garbage collected when needed, for instance when you do a intense fling. See solution here:

https://code.google.com/p/android/issues/detail?id=176328

Top app bars - Material Design, <androidx.coordinatorlayout.widget. Toolbar.Primary" /> </com.google. android.material.appbar.AppBarLayout> <!-- Note: A RecyclerView can also be � I want use android.support.design.widget.CoordinatorLayout, android.support.design.widget.AppBarLayout, android.support.design.widget.CollapsingToolbarLayout in my xml file. What are the possibilities to position a view below another view? for example : position ScrollView beneath ImageView in CoordinatorLayout. my xml code :

I'm using this codes before fragment changes.

scrollingElement.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
scrollingElement.dispatchNestedPreScroll(0, -Integer.MAX_VALUE, null, null);
scrollingElement.stopNestedScroll();

Using CoordinatorLayout in Android apps, How to use CoordinatorLayout in different ways to simplify Android app development, along with an In the sample below, we check for the position of the Toolbar relative to the CircleImageView, and Can you fix this? 2. FAB follows Widget. Unlike the previous example, this time the CoordinatorLayout is not the root layout. Rather, the root layout is a RelativeLayout, and the CoordinatorLayout is it’s only child.

CoordinatorLayout, By making use of the CoordinatorLayout in conjunction with the AppBarLayout and CollapsingToolbarLayout containers, the app bar can be configured to� How to reset the Toolbar position controlled by the CoordinatorLayout? Show CollapsingToolbarLayout Title ONLY when collapsed ; Collapsible Toolbar: Set how much the toolbar should be collapsed in onCreate ; CollapsingToolbarLayout without shadow in expanded state

Working with the Android AppBar and Collapsing Toolbar Layouts , It has additional level of control over it's child views. Note : CoordinatorLayout can also expand the ToolBar to show more content or collapse it while Now let's add a Button widget at the bottom of our screen. How to fix this error? Mastering the Coordinator Layout 27 Oct 2015. In the Google I/O 15, Google released a new support library which implements several components closely related with the Material Design’s spec, among these components you can find new ViewGroups like the AppbarLayout, CollapsingToolbarLayout and CoordinatorLayout.

Android CoordinatorLayout, Let's consider Toolbar that is not child of the AppBarLayout and it is overlayed over so the siblings can move up when AppBarLayout offsets receives the nested scroll events. CoordinatorLayout> <android.support.v7.widget.Toolbar Gradual elevation is controlled by the tiny space we are left and the� If you use different Fragments inside your CoordinatorLayout you will face the problem, that some Fragments have scrollable content and some should not scroll. Your Toolbar has the scrolling flags "scroll|enterAlways", which is ok for the former layouts, but not ok for the latter.

Comments
  • did you find a solution for this?
  • no not yet, a kind person had answered here, he deleted his post before even I had chance to discuss, he said that he used setTranslationY() on AppBarLayout to manipulate it rather than reset, and mentioned that it should work, which didn't for me, still looking for answers to this. I'll definitely post a solution once I find it.
  • Hey, @razzledazzle, I am trying to put a fragment with recyclerview in a FrameLayout which is part of the CoordinatorLayout. I specified layout_behavior in FrameLayout, but the AppBarLayout does not scroll. Anything you are doing differently? Do I have to initialize views in my activity?
  • fixed it. Forgot to add app:layout_scrollFlags="scroll" to toolbar.
  • One more thing. Even if I have no items in the recyclerview, toolbar still scrolls. Any idea how to fix that?
  • For me it only works for collapsing AppBarLayout by setting positive 'velocityY' value. Above code with negative 'velocityY' value has no effect. I'm calling it from NestedScrollView's onTouchListener after ACTION_UP action. I want to implement snap behavior - to always collapse or expand AppBarLayout.
  • Thanks for the answer, this is a good solution, I used the onNestedFling() inside the drawer open callback and it works pretty nicely.
  • I have exactly the same experience like @theb1uro. Only work for collapse with positive 'velocityY' value. Expand with negative 'velocityY' value has no effect...
  • I tested it also on cheesesquare and the same result, any idea why this work only when collapsing AppBarLayout?
  • params.getBehavior() is returning null. In xml, layout_behaviour tag is set on sibling NestedScrollView, not the AppBarLayout. Please help!
  • This does not cover the use case when you would want to lock the AppBarLayout.
  • AppBarLayout has no such method setExpanded?
  • @MuhammadBabar Update your dependencies, bro.