Narayan Panthi
1 min readJan 6, 2021

--

Make Bottom Navigation work with ConstraintLayout & CoordinateLayout

With navigation component & bottom navigation bar.

Photo by Author

In this article, we are going to implement bottom navigation with fragment using ConstraintLayout & CoordinateLayout

PROBLEM STATEMENT

I was having an issue with constraining views to the bottom app bar. It was necessary to use bottom navigation with coordinate layout. So here i will explain how i fixed it.

Lets Create our UI

dashboard_activity.xml with constraint layout as root looks like.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">


<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize"
app:defaultNavHost="true"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
app:navGraph="@navigation/mobile_navigation" />


<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
app:elevation="0dp"
app:fabAlignmentMode="center"
app:fabAnimationMode="scale"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">

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


</com.google.android.material.bottomappbar.BottomAppBar>


<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_add"
app:backgroundTint="@color/colorPrimary"
app:elevation="5dp"
app:fabSize="normal"
app:layout_anchor="@id/bottomNavigationView" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

When implementing it, The view hides under the bottom navigation. So, i gave a margin to fix it

Yeah, you will notice

android:layout_marginBottom =”?attr/actionBarSize”

This did the trick.

Thank you. Have a great day. :)
Thank you
#staysafe

--

--