Search code examples
androidandroid-coordinatorlayoutbottomnavigationviewandroid-snackbar

BottomNavigationView moves when Snackbar appears


I have a BottomNavigationView inside a CoordinatorLayout with app:layout_behavior=".BottomNavigationBehavior" to hide it when scrolling. The problem is that when I show a Snackbar, the BottomNavigationView moves up to make space for the Snackbar and then the Snackbar appears behind the BottomNavigationView. This behaviour happens only if I show a Snackbar while the BottomNavigationView is hidden after a scroll.

My goal is to have the SnackBar appear on top of the BottomNavigationView.

These are some screenshots (I can post a gif if you need):

For a split second the behaviour is right

For a split second the behaviour is right

Then the BottomNavigationView moves up with a transition

Then the BottomNavigationView moves up with a transition

Then when the Snackbar disappear the BottomNavigationView stays there until I scroll

Then when the Snackbar disappear the BottomNavigationView stays there until I scroll

This is my code:

activitymain.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_coordinator"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.bluemango.globe.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@android:color/white"
            android:gravity="center_vertical"
            app:titleTextAppearance="@style/MainToolbarText"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:theme="@style/ToolbarColor"
            app:layout_scrollFlags="scroll|enterAlways" >

            <ImageView
                android:id="@+id/search_iv"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_search_grey600_24dp"
                android:contentDescription="@string/search"
                android:clickable="true"
                android:focusable="true"
                android:onClick="searchArticle"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:layout_gravity="center"
                android:id="@+id/toolbar_title"
                android:textSize="20sp"
                android:textColor="@color/defSecondary"
                android:textStyle="bold"/>

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

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

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        android:background="@android:color/white"
        app:layout_constraintBottom_toBottomOf="@id/main_coordinator"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation"
        app:itemIconTint="@drawable/bottom_nav_colors"
        app:itemTextColor="@drawable/bottom_nav_colors"
        app:layout_behavior=".BottomNavigationBehavior"/>

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

The code of the Snackbar (created for a Fragment)

Snackbar snackbar = Snackbar
            .make(getActivity().findViewById(R.id.main_coordinator), getResources().getString(R.string.article_added_to_fav),
                    Snackbar.LENGTH_LONG)
            .setAction(android.R.string.cancel, new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    // onClick method.
                }
            })
            .setActionTextColor(ContextCompat.getColor(getActivity(), R.color.colorAccent));
    snackbar.show();

Thanks for the help!


Solution

  • I changed the Behavior class removing any reference to the Snackbar. Then I applied a marginBottom of the height of the BottomBar to the Snackbars. In the Behavior class, I added a method to show the BottomBar, so in case I need to show a Snackbar with the BottomBar hidden because of a scroll, I show the BottomBar with my new method then I show the Snackbar with the added margin.