Mike Kng
Mike Kng

Reputation: 285

android - collapsing toolbar with tablayout set background

My app contains a collapsing toolbar with a tabview. The tabs have an own background color, my @color/colorPrimary and the collapsing toolbar has a set background image. Now I would like to 'merge' them: the tablayout should be transparent, so the background image reaches from the toolbar down including the tabs. Like it is not divided by the background anymore. I set up the layout and tried to make the tabs transparent, but like this the image won't continue.

My XML file:

 <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"
    android:fitsSystemWindows="true"
    tools:context="com.example.te.e6.MainActivity">

    <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/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/colbar"
            android:layout_width="match_parent"
            android:layout_height="175dp"
            app:expandedTitleGravity="center|bottom"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/backg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/materialbackground"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:expandedTitleGravity="center|bottom"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            app:layout_anchor="@id/appBar"
            app:layout_anchorGravity="bottom">

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

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

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

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

Upvotes: 1

Views: 2907

Answers (2)

Patrick Lang
Patrick Lang

Reputation: 751

I have also been searching for a solution and I stumbled across this approach that helped me a bit. Finally I got it working. Here I post my solution using the latest Material3:

<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:id="@+id/list_appbarlayout_tabs">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/list_collapsing_toolbar_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            app:contentInsetStart="0dp"
            app:layout_scrollFlags="scroll|enterAlways|snap">

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tablayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabMode="fixed">

                    <com.google.android.material.tabs.TabItem
                        android:id="@+id/tab1"
                        android:layout_width="wrap_content"
                        android:layout_height="20dp" />

                    <com.google.android.material.tabs.TabItem
                        android:id="@+id/tab2"
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"/>

                    <com.google.android.material.tabs.TabItem
                        android:id="@+id/tab3"
                        android:layout_width="wrap_content"
                        android:layout_height="40dp" />

                </com.google.android.material.tabs.TabLayout>
            </com.google.android.material.appbar.MaterialToolbar>
        </com.google.android.material.appbar.AppBarLayout>


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


            <ImageView
                android:id="@+id/list_background"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:alpha="0.2"
                android:padding="24dp"
                app:srcCompat="@drawable/bg" />

            <com.google.android.material.progressindicator.LinearProgressIndicator
                android:id="@+id/list_progress_indicator"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:indeterminate="true" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/list_progress_indicator"
                android:layout_marginEnd="8dp">


            </androidx.recyclerview.widget.RecyclerView>


        </RelativeLayout>

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/list_bottom_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:hideOnScroll="true"
            app:menu="@menu/bottombar"
            />

        
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/list_fab_add_contentdesc"
            app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
            app:layout_anchor="@id/list_bottom_bar"
            app:srcCompat="@drawable/ic_add"
            />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

Pay special attention to

 app:layout_scrollFlags="scroll|enterAlways|snap">

For the Material Toolbar

and

 app:layout_behavior="@string/appbar_scrolling_view_behavior">

for the relative layout!

EDIT: There is a stupid margin at the start of the menu, to get rid of it add app:contentInsetStart="0dp" which is set to 16 dp by default (updated also the layout above)

Upvotes: 1

Eugene H
Eugene H

Reputation: 3568

Here is a working example:

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginBottom="56dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <ImageView
                android:id="@+id/backg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher_background"
                app:layout_collapseMode="parallax" />


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_marginBottom="48dp"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />


            <android.support.design.widget.TabLayout
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_gravity="bottom">

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

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

                <android.support.design.widget.TabItem
                    android:id="@+id/tabItem3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="C" />
            </android.support.design.widget.TabLayout>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

Upvotes: 0

Related Questions