material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.38k stars 3.07k forks source link

[Snackbar] Snackbar bottom margin is incorrect when using window insets with padding #3446

Open aleksandra-krzemien opened 1 year ago

aleksandra-krzemien commented 1 year ago

Description: Snackbar with anchor has incorrect bottom margin when window insets are used that set container padding instead of margin. When insets update margin, snackbar is shown correctly above the anchor button.

Zrzut ekranu 2023-06-9 o 15 54 13

When insets update padding, snackbar gets extra bottom margin. It seems that navigation bar height is used twice.

Zrzut ekranu 2023-06-9 o 15 54 27

Expected behavior: Snackbar should always be displayed right above the anchor.

Source code: The issue is most probably somewhere in BaseTransientBottomBar, but I wasn't able to identify exact place.

Minimal sample app repro: https://github.com/aleksandra-krzemien/nested-nav-graphs-example/tree/krzemien/snackbar-inset-handling Tap on to first fragment button to open FirstFragment. Current code will use insets with margin, so you will see situation on the first screenshot above (correct).

If you comment FirstFragment lines 41-43 and uncomment line 45, insets will update padding, and you'll end up with situation from second screenshot (incorrect).

Android API version: 29

Material Library version: 1.9.0

Device: Pixel 4a

UweTrottmann commented 1 year ago

I'm seeing something similar since the 1.7.0 release and going edge-to-edge, but without an anchor view: with a CoordinatorLayout and BottomNavigationView stacked in a LinearLayout (see below).

The culprit seems to be the OnApplyWindowInsetsListener which always adds the bottom window inset as bottom margin to the BaseTransientBottomBar. Basically, the Snackbar assumes it always is aligned with the bottom of the window.

Question is if this is considered a bug or a layout/configuration issue (in which way)?

https://github.com/material-components/material-components-android/blob/cbb380df61f8eb66273043b437582c8058df7088/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java#L396-L410 https://github.com/material-components/material-components-android/blob/cbb380df61f8eb66273043b437582c8058df7088/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java#L479-L483

Example layout, the Snackbar is launched with the CoordinatorLayout as the parent view:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <include layout="@layout/top_app_bar" />

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

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginStart="0dp"
            android:layout_marginTop="0dp"
            android:layout_marginEnd="@dimen/fab_margin"
            android:layout_marginBottom="@dimen/fab_margin"
            app:srcCompat="@drawable/ic_add_white_24dp" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/bottom_navigation_menu" />

</LinearLayout>
Snackbar.make(findViewById(R.id.coordinatorLayout), "message", Snackbar.LENGTH_LONG).show()
Funambol-Android-Developers commented 3 months ago

Good morning! Any update on this bug? It seems still present in 1.12.0 Is there any workaround? Edge-to-edge is going to be inforced with targetSdk = 35 ...

Kuglll commented 3 weeks ago

Any updates here? We are also experiencing the same issue.