Android how to add collapsing toolbar with layout body touch collapse and expand

Home  >>  Tutorials  >>  Android  >>  Android how to add collapsing toolbar with layout body touch collapse and expand

Android how to add collapsing toolbar with layout body touch collapse and expand

15
May,2018

0

1 .

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.adn.activities.root.HomeActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
       >



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

            >


            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay" >



                    <!--<ImageView-->
                    <!--android:src="@drawable/logo"-->
                    <!--android:layout_width="@dimen/count_60dp"-->
                    <!--android:layout_height="wrap_content" />-->

                </android.support.v7.widget.Toolbar>





            </LinearLayout>




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


        <EditText
            android:id="@+id/search_bar"
            android:background="@drawable/corner_radious"
            android:layout_width="match_parent"
            android:editable="false"
            android:layout_height="@dimen/count_40dp"
            android:paddingLeft="@dimen/count_10dp"
            android:drawableLeft="@drawable/search_gray_24"
            android:hint="Search for Products, Brands and More "
            android:textSize="@dimen/count_12dp"
            android:drawablePadding="@dimen/count_10dp"
            android:textColorHint="@color/dark_gray"
            android:textColor="@color/dark_gray"
            android:layout_marginLeft="@dimen/count_10dp"
            android:layout_marginRight="@dimen/count_10dp"
            android:layout_marginBottom="@dimen/count_5dp"

            />

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

    <include layout="@layout/content_home2" />



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



2. In content layout add inner scroll view 


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 
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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.adn.activities.root.HomeActivity"
    tools:showIn="@layout/app_bar_home">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>



3 . Call this method in onCreate 

private void setAppBarDragging(final boolean newValue) {
    AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);
    CoordinatorLayout.LayoutParams params =
            (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
    AppBarLayout.Behavior behavior = new AppBarLayout.Behavior();
    behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
        @Override
        public boolean canDrag(AppBarLayout appBarLayout) {
            return newValue;
        }
    });
    params.setBehavior(behavior);
}


and done :)

Leave a Reply

Your email address will not be published. Required fields are marked *