Snackbar and CollapsingToolbarLayout with TranslucentNavigation

4.3k views Asked by At

I have Created an app with Scrolling Activity from the Android Studio Templets to test my coding of my main App affects that behavior or not, so I have just add to the code :

<item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item>

that shows the Snackbar behind the navigation bar as this screenshot (PS : I am using Xstane for redesigning my Navigation bar on my mobile but I think that does not affect the code cuz i have tried TranslucentNavigation with Snackbar Without CollapsingToolbarLayout and that works well)

enter image description here

the App is supporting

  • windowTranslucentNavigation
  • Snackbar
  • CollapsingToolbarLayout
  • FloatingActionButton

And This is the code of the main 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".testscanbarwithcollapsing.ScrollingActivity"
>

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    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:layout_scrollFlags="scroll|exitUntilCollapsed"
        >

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

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

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|end"
    app:srcCompat="@android:drawable/ic_dialog_email"
    />

UPDATED : here is the code of the onClick of the FloatingButton that show the Snackbar (this code is in onCreate of the main activity)

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            fabProgressCircle.show();
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
1

There are 1 answers

5
Alexandr Larin On BEST ANSWER
    private static void show(final Activity activity, Snackbar snackbar) {
    if (ScreenUtils.isTranslucentNavigationBar(activity)){
        final FrameLayout snackBarView = (FrameLayout) snackbar.getView();
        final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) snackBarView.getChildAt(0).getLayoutParams();
        params.setMargins(params.leftMargin,
                params.topMargin,
                params.rightMargin,
                params.bottomMargin + ScreenUtils.getNavigationBarHeight(activity));

        snackBarView.getChildAt(0).setLayoutParams(params);
    }
    snackbar.show();
}

public static boolean isTranslucentNavigationBar(Activity activity) {
    Window w = activity.getWindow();
    WindowManager.LayoutParams lp = w.getAttributes();
    int flags = lp.flags;
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
            && (flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
            == WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;

}

This ideal works for me!