Search code examples
androidfloating-action-buttonandroid-bottomappbar

How to handle Bottomappbar items selector?


I'm currently working with the BottomAppBar for my Music app, with 4 menu items stay on the left and the FAB anchored on the right, each of the item will navigate to one function Fragment.

I want to set a selector to all 4 of these Menu items which is similar to BottomNavigationView that will have 2 states when selected and not selected. But so far I'm struggling to do it.

I already tried the selector in XML or programmatically setting it in code but none of these worked.

Here is the layout I want to achieve, sorry can't post the direct image because I do not have enough reputation.
https://i.sstatic.net/c0OiL.jpg


Solution

  • Edit 1:

    Okay great, so I just checked it out and you will not be able to change the color of icons in the bottomappbar when selected similar to BottomNavigationView. But there's a hack around for it where you can add a BottomNavigationView alongside the BottomAppBar..

    <androidx.coordinatorlayout.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">
    
        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottom_app_bar"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_gravity="bottom"
            app:backgroundTint="#008577"
            app:fabAlignmentMode="end">
    
               <com.google.android.material.bottomnavigation.BottomNavigationView
                  android:id="@+id/navigation1"
                  android:layout_width="match_parent"
                  android:layout_height="56dp"
                  android:background="?android:attr/windowBackground"
                  app:itemBackground="@android:color/white"
                  app:itemIconTint="@drawable/nav_item_colors"
                  app:itemTextColor="@drawable/nav_item_colors"
                  app:menu="@menu/navigation" />
    
      </com.google.android.material.bottomappbar.BottomAppBar>
    
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_attach_money_black_24dp"
            app:layout_anchor="@id/bottom_app_bar" />
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    In your case, create five menu items and disable the fifth menu item in the bnv. So your fab will appear in that place.

    Old Answer:

    Try this out:

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation1"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:itemBackground="@android:color/white"
        app:itemIconTint="@drawable/nav_item_colors"
        app:itemTextColor="@drawable/nav_item_colors"
        app:menu="@menu/navigation" />
    

    In drawable directory, create nav_item_colors:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="#E15B49" android:state_checked="true" />
        <item android:color="@color/colorviolet" android:state_checked="false" />
    </selector>