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)?
Place bottomAppBar under your bottomNavigationView with transparent background. And add empty menu item to menu.xml to free space for the FAB.
XML:
Result
This might help someone to achieve the above design. Add empty menu item to adjust the fab button and space.
The team that implement the material components for android says that this design is not part of the specification, but they generously offer a simple solution that can be seen in the link below.
Is it possible to have the BottomAppBar & FloatingActionButton with menu evenly distributed? #72
Basically, one need to change the layout params of the container of the menu buttons:
The combination of this with your void menu item will do the trick, avoiding the use of another android component.
1 - Include Maven in your repository in
build.gradle
2 - Place material components dependency in your
build.gradle.
Keep in mind that material version is regularly updating.3 - Set compileSdkVersion, and targetSdkVersion to the latest API version targetting Android P which is 28.
4 - Make sure your app inherits
Theme.MaterialComponents
theme in order to makeBottomAppBar
use the latest style. Alternatively you can declare the style forBottomAppBar
in widget declaration within layout xml file as follows.5 - You can include BottomAppBar in your layout as follows. BottomAppBar must be a child of CoordinatorLayout.
6 - You can anchor a Floating Action Button (FAB) to BottomAppBar by specifying the id of the BottomAppBar in app:layout_anchor attribute of the FAB. BottomAppBar can cradle FAB with a shaped background or FAB can overlap BottomAppBar.
7 - There are many attributes you can use to configure the Bottom Nav Bar and the Fab Icon.
8 - Check this medium post for a more complete explanation.
UPDATE: Check the OP answer for the final solution for his particular problem.
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.