Search code examples
androidmaterial-designandroid-appbarlayoutbottomnavigationviewandroid-bottomappbar

How to implement new material BottomAppBar as BottomNavigationView


I was trying to implement the new BottomAppBar that usually looks like this: material BottomAppBar as a BottomNavigationView like in the Google home app that looks like this.

My problem is that since I can fill the BottomAppBar only with a menu resource, I can't understand how to align my buttons to look like a BottomNavigationView (but with the "cut" for the Fab button) instead of align everything to one side of the BottomAppBar.

How can I add a custom layout inside this new Material BottomAppBar?

Or instead, is there any way to implement a BottomNavigationView with the "cut" for for the Fab button (keeping cool default animations like the new BottomAppBar)?


Solution

  • SOLVED

    Basically, instead of trying to force the menu resources to the layout that i needed, i used this solution instead, i just put a LinearLayout inside the BottomAppBar using the "empty" element as @dglozano suggested.

    Using ?attr/selectableItemBackgroundBorderless i'm also able to achieve an effect that is really similar to the BottomNavigationView.

    Screenshot of BottomAppBar

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:gravity="center"
        app:layout_anchorGravity="start"
        app:hideOnScroll="true"
        app:fabAnimationMode="scale"
        app:fabAlignmentMode="center"
        app:contentInsetEnd="16dp"
        app:contentInsetStart="16dp"
        app:backgroundTint="@color/colorPrimary">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="5">
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:srcCompat="@drawable/ic_home_white_24dp"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:tint="@color/secondary_text"/>
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:srcCompat="@drawable/ic_map_black_24dp"
                android:background="?attr/selectableItemBackgroundBorderless"/>
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@android:color/transparent"/>
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:srcCompat="@drawable/ic_people_white_24dp"
                android:background="?attr/selectableItemBackgroundBorderless"/>
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:srcCompat="@drawable/ic_account_circle_24dp"
                android:background="?attr/selectableItemBackgroundBorderless"/>
        </LinearLayout>
    </com.google.android.material.bottomappbar.BottomAppBar>