Search code examples
androidxmlkotlinbottomnavigationviewandroid-snackbar

Move Snackbar above the bottomNavigationView


Want to move my snackbar above bottomnavigationview

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activityRootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbarView"
                android:layout_width="match_parent"
                android:layout_height="@dimen/action_bar_size"
                android:background="@color/color_primary"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
        </FrameLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottomNavigationLayout"
        android:layout_below="@+id/appBarLayout" />

    <LinearLayout
        android:id="@+id/bottomNavigationLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

        <View style="@style/FullDivider" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:itemBackground="@android:color/white"
            app:itemIconTint="@drawable/selector_bottom_bar_item"
            app:itemTextColor="@color/blue"
            app:labelVisibilityMode="unlabeled"
            app:menu="@menu/main_navigation" />
    </LinearLayout>

    <com.mandarine.features.security.UnlockAppInputView
        android:id="@+id/unlockAppInputView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true" />
</RelativeLayout>

In MainActivity write smth like this:

container?.let {
    Snackbar.make(it, "No internet connection!", Snackbar.LENGTH_LONG).show()
}

But snackbar also display from bot of screen


Solution

  • Resolve my problem with next solution:

    private fun showNetworkMessage(isConnected: Boolean) {
        if (!isConnected) {
            val snack = Snackbar.make(
                findViewById(R.id.container),
                this.getText(R.string.warning_no_internet_connection), Snackbar.LENGTH_LONG
            )
            val params = snack.view.layoutParams as FrameLayout.LayoutParams
            params.setMargins(0, 0, 0, this.resources.getDimension(R.dimen.action_bar_size).toInt())
            snack.view.layoutParams = params
            snack.show()
        }
    }
    

    Where u can see i didn't use CoordinatorLayout.