I'm attempting to implement a BottomAppBar within my Android App.
The MainActivity XML looks like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:id="@+id/coordinator"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/rpurple"
android:backgroundTint="@color/reallyDarkPurple"
style="@style/Widget.MaterialComponents.BottomAppBar.PrimarySurface"
app:menu="@menu/bottom_nav_menu"
app:tint = "@color/white"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/navigation_profile"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/user"
android:padding="5dp"
app:tint = "@color/lightPurple"
android:scaleType="centerCrop"
android:background="@android:color/transparent"
android:adjustViewBounds="true"/>
<ImageButton
android:id="@+id/historyBtn"
android:layout_width="60dp"
android:padding="5dp"
android:layout_marginEnd="10dp"
android:layout_height="60dp"
android:src="@drawable/clock"
android:layout_alignParentEnd="true"
app:tint = "@color/lightPurple"
android:scaleType="centerCrop"
android:background="@android:color/transparent"
android:adjustViewBounds="true"/>
</RelativeLayout>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="56dp"
android:layout_height="56dp"
android:backgroundTint="@color/lightPurple"
app:srcCompat="@drawable/addwhite"
app:tint = "@color/white"
app:borderWidth="0dp"
android:adjustViewBounds="true"
app:fabCustomSize="56dp"
app:layout_anchor="@id/bottomAppBar"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>
And i've implemented the code like so within my MainActivity:
BottomAppBar bottomAppBar = findViewById(R.id.bottomAppBar);
bottomAppBar.replaceMenu(R.menu.bottom_nav_menu);
setSupportActionBar(bottomAppBar);
Ideally what I'd like to do is navigate between 4 seperate fragments, A,B,C,D using the BottomAppBar's ImageButtons R.id.navigation_profile
& R.id.historyBtn
A key part of the requirement is that the buttons are on opposite sides of the screen.
How could I handle the OnClick
for each imageButton and then the subsequent fragment transition?
What I understand that you want to add a navigation that have 4 fragment to the bottomAppBar. You can do it by remove the relative layout that in the BottomAppBar and include it as an item in a menu like this
first add that to the dependencies
your main Activity
and the menu file should look something like this
add this to the style file
you can handle the item on the bottom like that
and you can use this to decide what fragment show when you open the activity put in
onCreate