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

[AppBarLayout] fitsSystemWindows="true" is ignored if wrapped inside CoordinatorLayout without fitsSystemWindows #4541

Open
RoiEXLab opened this issue Jan 18, 2025 · 0 comments

Comments

@RoiEXLab
Copy link

Description: With SDK 35 I had another look at edge-to-edge interactions. In my case I have a androidx.coordinatorlayout.widget.CoordinatorLayout as the root layout with a nested com.google.android.material.appbar.AppBarLayout that has android:fitsSystemWindows="true". The activity sets WindowCompat.setDecorFitsSystemWindows(getWindow(), false); on creation. However, android:fitsSystemWindows doesn't seem to have any influence on the visuals, so any text from the toolbar (and potential menu controls) are behind transparent controls (if present).

As a workaround I wrote this snippet to apply the required padding manually:

protected void addSystemPaddingToComponentNoBottom(View view) {
    int originalLeftPadding = view.getPaddingLeft();
    int originalTopPadding = view.getPaddingTop();
    int originalRightPadding = view.getPaddingRight();
    int originalBottomPadding = view.getPaddingBottom();

    ViewCompat.setOnApplyWindowInsetsListener(view, (v, windowInsets) -> {
        Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout());
        int leftPadding = originalLeftPadding + insets.left;
        int topPadding = originalTopPadding + insets.top;
        int rightPadding = originalRightPadding + insets.right;

        view.setPadding(leftPadding, topPadding, rightPadding, originalBottomPadding);

        return windowInsets;
    });
}

If I apply this to all com.google.android.material.appbar.AppBarLayouts in my app, everything works as expected.

Expected behavior: I would android:fitsSystemWindows="true" to apply this inset by itself. it works without issues when using it on the root CoordinatorLayout (which I want to be visible behind the controls, so that's why this is not an option).

Source code:

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

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

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/top_app_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" />

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:importantForAccessibility="no"
        android:paddingStart="@dimen/recycler_view_outer_padding"
        android:paddingEnd="@dimen/recycler_view_outer_padding"
        android:paddingBottom="80dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/add_pack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="@dimen/floating_action_button_margin"
            android:contentDescription="@string/add_new"
            android:onClick="add"
            android:src="@drawable/plus_symbol" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Please not how android:fitsSystemWindows="true" works for the nested CoordinatorLayout without any issues.

Android API version: 35

Material Library version: 1.12.0

Device: Probably all of them, tested in Emulator with Pixel 4a API 31

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

2 participants