Search code examples
androidandroid-layoutandroid-coordinatorlayoutfloating-action-buttonbottomnavigationview

Bottom Navigation with fab


I am currently working with BottomNavigationView and FloatingActionButton. What i want to achieve is this below design:

enter image description here

And what i have tried:

<?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"
    app:layout_insetEdge="bottom"
    tools:context=".activity.BottomNavPrimary">

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|bottom" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_insetEdge="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_primary"></android.support.design.widget.BottomNavigationView>

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

Solution

  • Your design looks as if you want to use the new BottomAppBar from the MaterialComponents that will fully be release with Android P. Especially if the left icon stands for a kind of side navigation it could be the right navigation element.

    However, you have to be aware, that the elements on the left and right of the FAB have a different purpose than a bottom navigation. Instead of being entry points to "primary destinations" in an app, the BottomAppBar is defined as:

    A bottom app bar displays navigation and key actions at the bottom of mobile screens.

    So it is for page actions (like opening the dashboard or search). More explanation can be found in the design documentation.

    I haven't had a chance to implement it yet (because I really needed it with a bottom navigation), but here is the code documentation example:

    <android.support.design.widget.CoordinatorLayout
        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">
    
      <!-- Other components and views -->
    
      <com.google.android.material.bottomappbar.BottomAppBar
          android:id="@+id/bar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="bottom"
          app:navigationIcon="@drawable/ic_menu_24"/>
    
      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_anchor="@id/bar"/>
    
    </android.support.design.widget.CoordinatorLayout>
    

    So to me, it sounds as if you define your two page menu options and since the FAB is anchored to the bar it will push them to the sides.

    The documentation also includes options for the optional FAB cradle that was shown during this year's Google I/O and shows how to handle menu and click handling.

    Here is another useful link on how to set up gradle to include the new material components in your project.