Search code examples
androidandroid-layoutandroid-toolbarandroid-collapsingtoolbarlayoutappbar

How do I place toolbar below collapsingtoolbarlayout when expanded?


I have the following code below on which I'm trying to make a collapsible toolbar that will make the toolbar which has a linearlayout inside, to stick to the bottom part of the layout when expanded.

I have been moving parts/layouts all around for several hours trying to make it stick to the bottom and still show at the appbar even when closed/pushed up but I cant make it show up as I intended.

<android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/ivProfileAvatar"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/profile_image" />

            <Space
                android:layout_width="match_parent"
                android:layout_height="16dp" />

            <TextView
                android:id="@+id/tvUserUsername"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="SomeName"
                android:textColor="@android:color/white"
                android:textSize="24dp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="UniversityName"
                android:textColor="@android:color/white"
                android:textSize="16dp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="tvUserStatus"
                android:textColor="@android:color/white"
                android:textSize="16dp" />

            <Space
                android:layout_width="match_parent"
                android:layout_height="16dp" />

            <Button
                android:id="@+id/btnEditProfile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:background="@drawable/btn_gradient_blue"
                android:paddingEnd="48dp"
                android:paddingStart="48dp"
                android:text="Edit Profile"
                android:textAllCaps="false"
                android:textColor="@android:color/white" />

        </LinearLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin">

            <android.support.design.widget.TabLayout
                android:id="@+id/tabsProfile"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:tabBackground="@null"
                app:tabGravity="center"
                app:tabIndicatorColor="@android:color/white"
                app:tabIndicatorHeight="2dp">

                <android.support.design.widget.TabItem
                    android:id="@+id/tabProfileMyLikes"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <android.support.design.widget.TabItem
                    android:id="@+id/tabProfileMyPosts"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

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

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

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

Solution

  • You are using TabLayout inside Toolbar and that's not a good design and wrong (i should say).

    Instead, try something like this:

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />
    
                <android.support.design.widget.TabLayout
                    android:id="@+id/tabsProfile"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:tabBackground="@null"
                    app:tabGravity="center"
                    app:tabIndicatorColor="@android:color/white"
                    app:tabIndicatorHeight="2dp">
    
                    <android.support.design.widget.TabItem
                        android:id="@+id/tabProfileMyLikes"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
    
                    <android.support.design.widget.TabItem
                        android:id="@+id/tabProfileMyPosts"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
    
                </android.support.design.widget.TabLayout>
    

    To make a view pinned or stick somewhere, using app:layout_collapseMode="pin" will help or instead, add the view to CoordinatorLayout like FloatingActionButton position to make it stick in the bottom and set: android:gravity to bottom.