I would like to create the view below for a test android application. These are the present steps that I have followed to create the view:
- Created layout file with the coordinator layout as the root element.
- Added the bottom bar and anchored it to the bottom.
- Added a floating action button and anchored it to the bottom bar.
- Created a menu resource file with three menu items.
- Added the menu item to the bottom bar.
When I run the application, the view looks like the image below:
I have tried using the Bottomnavigationview with the main floating action button being inflated as an action layout in the menu item. Using this approach only allows for the menu items and their test to be displayed but the floating action button is not displayed.
What would be the best approach to use. Using the BottomAppBar or using BottomNavigationView. I am trying to see if I can use the material components available without having to perform hacks.
The XML files are as follows:
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".MainActivity">
<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"
app:srcCompat="@mipmap/sharp_local_atm_black_24" />
<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:fabAlignmentMode="center"
app:fabCradleRoundedCornerRadius="0dp"
app:fabCradleMargin="0dp"
app:fabAnimationMode="slide"
app:labelVisibilityMode="labeled"
app:fabCradleVerticalOffset="8dp"
app:menu="@menu/app_bar_menu"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
The menu XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/app_bar_home"
android:icon="@mipmap/sharp_home_black_24"
android:title="@string/home"
app:showAsAction="always"/>
<item
android:id="@+id/app_bar_wallet"
android:icon="@mipmap/sharp_account_balance_wallet_black_24"
android:title="@string/wallet"
app:showAsAction="always"/>
<item
android:id="@+id/app_bar_atm"
android:title="CASH"
app:showAsAction="always"/>
<item
android:id="@+id/app_bar_cart"
android:icon="@mipmap/sharp_shopping_cart_black_24"
android:title="@string/cart"
app:showAsAction="always"/>
<item
android:id="@+id/app_bar_more"
android:icon="@mipmap/sharp_menu_black_24"
android:title="@string/more"
app:showAsAction="always"/>
</menu>