Search code examples
androidandroid-layoutandroid-coordinatorlayoutcoordinator-layout

Trigger AppBar back to its normal position inside CoordinatorLayout


I have the next layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_offer_details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways|snap" />

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

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/layout_offer_detail_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorBackground"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

        </>
    </>
</>

This will hide the app bar when the user scrolls down in the list and will show it when the user scrolls up.

My problem is that at the end of the NestedScrollView I have a "Back to top" button, which when clicked will trigger the scrollView to scroll back up top: scrollView.smoothScrollTo(0, 0);

The issue is that the appBar does not come back down as when the user would scroll to the top. I am unable to trigger the app bar back down programatically.

Any ideas ?!

EDIT

The functionality was taken from here: see link


Solution

  • It was easier than I thought. After three hours of searching and trying out things, I just had to do this:

    appBarLayout.setExpanded(true, true);
    

    Well, that was an unnecessary waste of time on my part...