Search code examples
androidlayoutandroid-toolbar

How to hide toolbar in collapsed mode and show in normal mode


I am trying to implement collapsing toolbar behaviour in my app with a tab layout. I want to hide the toolbar when in collapse mode and just show the tab layout in collapse mode and in normal mode until collapsed I want to show the toolbar with the title of the app. Currently, it works as I want in collapsed mode I just want to add a toolbar that hides in collapsed mode.

My code

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

    </data>



    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/htab_maincontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/htab_appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/htab_collapse_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                app:titleEnabled="true">

                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:gravity="center"
                                    android:orientation="vertical"
                                    android:background="@color/greyprofile"
                                    android:padding="16dp">
                                    <!--                    android:background="@drawable/circularbordersolid"-->

                    <de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/profilepic"
                        android:layout_width="150dp"
                        android:layout_height="150dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/hey" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="10dp"
                                android:fontFamily="@font/proximanovaregular"
                                android:gravity="center"
                                android:text="Name User"
                                android:textColor="#424242"
                                android:textSize="21sp"
                                android:textStyle="bold" />

                        </RelativeLayout>


                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/proximanovaregular"
                            android:text="Username"
                            android:textColor="#424242"
                            android:gravity="center"
                            android:textSize="14sp" />

                    </LinearLayout>


                    <Button
                        android:id="@+id/editProfileBtn"
                        android:layout_gravity="center"
                        android:layout_marginTop="10dp"
                        android:layout_marginStart="10dp"
                        android:layout_marginEnd="10dp"
                        android:paddingBottom="10dp"
                        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColor="#757575"
                        android:text="Edit Profile"
                        android:gravity="center"
                        android:textAllCaps="false"
                        android:textSize="16sp" />

                </LinearLayout>

<!--                <androidx.appcompat.widget.Toolbar-->
<!--                    android:id="@+id/htab_toolbar"-->
<!--                    android:layout_width="match_parent"-->
<!--                    android:layout_height="?attr/actionBarSize"-->
<!--                    android:layout_gravity="top"-->
<!--                    app:layout_collapseMode="pin"-->
<!--                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>-->
                <!--                android:layout_marginBottom="48dp"-->

            </com.google.android.material.appbar.CollapsingToolbarLayout>

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"/>
            <!--                android:background="@drawable/circularbordersolid"-->
            <!--                app:tabIndicatorColor="@android:color/white"-->
            <!--                app:tabSelectedTextColor="@android:color/white"-->


        </com.google.android.material.appbar.AppBarLayout>

        <androidx.viewpager2.widget.ViewPager2
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/pager"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

Solution

  • You should remove exitUntilCollapsed from your app:layout_scrollFlags

    So, do it like that app:layout_scrollFlags="scroll|snap"