0

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");
    }
hemi_p
  • 43
  • 9
  • 3
    What have you tried so far? It would help if you post some code that can be reproduced. – DSLima90 Mar 23 '18 at 12:40
  • I have added my xml code and activity code. Please check my edited message. img1 is image with 700x933 px. – hemi_p Mar 24 '18 at 04:30

2 Answers2

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme">


            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:fitsSystemWindows="true"
                app:contentScrim="@color/colorAccent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">


                <ImageView
                    android:id="@+id/img_user_photo"
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    android:src="@drawable/meeting_bg"
                    app:layout_collapseMode="parallax" />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@android:color/transparent"
                    app:contentInsetStartWithNavigation="0dp"
                    app:contentInsetStart="0dp"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                <include
                    layout="@layout/widget_header_view_top"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone" />
                </android.support.v7.widget.Toolbar>


            </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="match_parent"
                android:orientation="vertical">


            </LinearLayout>

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


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

</RelativeLayout>

For setting Toolbar title and sub title on collapsing... I have used layout which I included in toolbar ...check xml... "widget_header_view_top" You can customize as per your need.....

Java class:

 setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_nav_white);
    getSupportActionBar().setTitle("");
    collapsing_toolbar.setTitle(" ");

appbar.addOnOffsetChangedListener(this);

 @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        int maxScroll = appBarLayout.getTotalScrollRange();
        float percentage = (float) Math.abs(verticalOffset) / (float) maxScroll;

        if (percentage >= 0.6f && isHideToolbarView) {
            toolbarHeaderView.setVisibility(View.VISIBLE);
            if (menuItemEdit != null) {
                //   menuItemEdit.setIcon(R.drawable.edit_white);
            }
            isHideToolbarView = !isHideToolbarView;

        } else if (percentage < 0.6f && !isHideToolbarView) {
            if (menuItemEdit != null) {
                //menuItemEdit.setIcon(R.drawable.edit);
            }
            toolbarHeaderView.setVisibility(View.GONE);
            isHideToolbarView = !isHideToolbarView;
        }
    }
  • I want to open toolbar open at specific height eg 200px. Can you please explain how can I achieve with this code. – hemi_p Mar 24 '18 at 05:18
0

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);
Ram Koti
  • 2,203
  • 7
  • 26
  • 36
hemi_p
  • 43
  • 9