Snackbar and CollapsingToolbarLayout with TranslucentNavigation

I created a scroll function application from Android Studio Templets to verify that my encoding of my main application affects this behavior or not, so I’ll just add the code:

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

which shows Snackbar behind the navigation bar as this screenshot (PS: I use Xstane to reverse engineer my navigation bar on my mobile phone, but I think it doesn’t affect the code because I tried TranslucentNavigation with Snackbar Without CollapsingToolbarLayout and it works a )

enter image description here

application supports

  • windowTranslucentNavigation
  • Snackbar
  • CollapsingToolbarLayout
  • FloatingActionButton

And this is the main xml code

<?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"
    />

: onClick of FloatingButton, Snackbar ( onCreate )

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();
        }
    });
0
1
    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;

}

!

+7

Source: https://habr.com/ru/post/1664677/


All Articles