Search code examples
androidappbar

CollapsingToolbarLayout open with min height when image header contains large image


I have collapsing toolbar integrated. I want same functionality like [Set starting height of CollapsingToolbarLayout. I have tried same code as suggested here but did't find solution.

I am using compile sdk version 26.

Is anyone has implemented same thing or can help me to find solution?

<android.support.constraint.ConstraintLayout 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"
    tools:context="test_scroll.com.testscroll.MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/llmain_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="16dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/img_user_photo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true"
                    android:maxHeight="256dp"
                    android:src="@drawable/img1"
                    app:layout_collapseMode="parallax" />


                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:layout_scrollFlags="scroll|enterAlways"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


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

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

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nested"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


            <LinearLayout
                android:id="@+id/llMainNested"
                android:layout_width="match_parent"
                android:layout_height="1000dp"
                android:orientation="vertical">


            </LinearLayout>

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


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

</android.support.constraint.ConstraintLayout>

onCreate of activity

mCoordinatorLayour = (CoordinatorLayout) findViewById(R.id.llmain_content);
        mAppBarLayout = (AppBarLayout) findViewById(R.id.appbar);

        mAppBarLayout.post(new Runnable() {
            @Override
            public void run() {
                int heightPx = findViewById(R.id.img_user_photo).getHeight();
                setAppBarOffset(heightPx/2);
            }
        });


private void setAppBarOffset(int offsetPx){
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
        AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
//        behavior.onNestedPreScroll(mCoordinatorLayour, mAppBarLayout, null, 0, offsetPx, new int[]{0, 0});
        if(behavior!=null)
        behavior.onNestedPreScroll(mCoordinatorLayour, mAppBarLayout, null, 0, offsetPx, new int[]{0, 0}, 1);
        else
            Log.i("TAG", "setAppBarOffset: null");
    }

Solution

  • I got the answer with trial and error with above code of the setAppBarOffset method. In this method, I was setting offset which I want to keep visible on page load. Instead, we need to pass the offset which we need to hide. So I have added below code to keep 256 px of the image visible on creating.

    public static int getDeviceHeight(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int height = metrics.heightPixels;
    
        Rect rectgle = new Rect();
        Window window = ((Activity) context).getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
        int StatusBarHeight = rectgle.top;
        height = height - StatusBarHeight;
        return height;
    }
    
    
    
    public static int getMinHeightOfAppbar(int device_height,int view_height){
        int offsetPx = (int) (device_height * 0.45);
        offsetPx = view_height - offsetPx;
        return offsetPx;
    }
    
    int device_height = Utility.getDeviceHeight(ProfileSettingsNew.this);
    offsetPx= Utility.getMinHeightOfAppbar( device_height,imgv_user_photo.getHeight());
    
    setAppBarOffset(offsetPx);