Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CollapsingToolbar and MaterialToolbar height when adapting edge-to-edge enforcement #4482

Open
ArcherEmiya05 opened this issue Dec 18, 2024 · 0 comments

Comments

@ArcherEmiya05
Copy link

ArcherEmiya05 commented Dec 18, 2024

Description:
Handling window insets to adapt edge-to-edge enforcement of Android 15 requires the Toolbar height to be set as wrap_content since we usually add padding top on it via ViewCompat.setOnApplyWindowInsetsListener. This however is not possible when a Toolbar is inside CollapsingToolbar since setting the Toolbar's height to wrap_content will make its height matching the CollapsingToolbar height based on this previous issue.

Expected behavior: Enable edge-to-edge enforcement when a Toolbar collapsed using CollapsingToolbar.

This is edge-to-edge, it draws the whole content behind system bars despite the Toolbar height here is incorrect and to be fix due to this bug. The below image is for reference and is not using CollapsingToolbar.
image
image

Source code:
This code allows the drawing of ImageView behind status bar but when we collapse the Toolbar, it is not edge-to-edge.

<androidx.coordinatorlayout.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=".presentation.NewsDetailActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/collapsingToolbarLayoutLargeSize"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|snap|enterAlways|enterAlwaysCollapsed"
            app:maxLines="3">

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/featuredImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:fitsSystemWindows="true"
                android:src="@drawable/logo_dynamic_placeholder"
                android:background="@color/mdc_colorWhiteDark_Primary"
                app:layout_collapseMode="parallax" />

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                app:navigationIcon="?attr/homeAsUpIndicator"
                app:navigationContentDescription="@string/abc_action_bar_up_description"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|snap|enterAlways|enterAlwaysCollapsed"
                app:title="@tools:sample/cities" />

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

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

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <org.sufficientlysecure.htmltextview.HtmlTextView
                android:id="@+id/description_html_txt_v"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingHorizontal="@dimen/space_12"
                android:paddingTop="@dimen/space_48"
                android:paddingBottom="@dimen/space_12"
                android:text="@tools:sample/lorem/random"
                android:textSize="@dimen/textSize14" />

            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingVertical="@dimen/space_16"
                android:visibility="visible">

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/labelTxt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingHorizontal="@dimen/space_12"
                    android:paddingBottom="@dimen/space_8"
                    android:text="@string/related_content"
                    android:textAllCaps="true"
                    android:textColor="@color/mdc_colorPrimaryDark_Accent"
                    android:textSize="@dimen/textSize24" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/feedRecycler"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clipToPadding="false"
                    android:nestedScrollingEnabled="false"
                    android:paddingHorizontal="@dimen/space_12" />

            </androidx.appcompat.widget.LinearLayoutCompat>

        </androidx.appcompat.widget.LinearLayoutCompat>

    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/visitWeb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:useCompatPadding="true"
        android:contentDescription="@string/news"
        android:src="@drawable/ic_open_tab"
        app:layout_anchor="@id/appBar"
        app:layout_anchorGravity="bottom|end" />

    <FrameLayout
        android:id="@+id/ad_frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|bottom" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

This is not edge-to-edge, the content is being cut and not drawn behind system bars.
image
image
image

Attempt 1

What I did so far is to remove fitsSystemWindows=true in AppBarLayout, which resulted to this.
image
image

Adding the runtime padding adjustment.

ViewCompat.setOnApplyWindowInsetsListener(root) { _: View, insets: WindowInsetsCompat ->
    val barInset = insets.getInsets(WindowInsetsCompat.Type.systemBars())
    // Add padding to this view based on status bar inset
    toolbar.updatePadding(top = barInset.top)
    // Return WindowInsetsCompat.CONSUMED if you don't want the window insets to keep passing down
    // to descendant views including all views of Fragment(s) under this Fragment
    WindowInsetsCompat.CONSUMED
}

Conclusion 1

Almost but the collapsed Toolbar height is in mess.
image
image
image

Minimal sample app repro: N/A

Android API version: Android 15

Material Library version: 1.12.0

Device: AVD/Emulator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants