Karan Mehta
Karan Mehta

Reputation: 1541

How to provide proper margin according to FAB in BottomAppBar kotlin andorid?

I want to have FAB in between cutout of bottom app bar and I'm following this link to do so :

https://www.geeksforgeeks.org/how-to-add-a-floating-action-button-to-bottom-navigation-bar-in-android/

Here is what I have done in code :

<?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=".activities.TabBarWithFloatingButton">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:fabCradleMargin="10dp"
        app:fabAlignmentMode="center"
        app:fabAnimationMode="slide"
        app:fabCradleRoundedCornerRadius="10dp"
        app:fabCradleVerticalOffset="10dp">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="16dp"
            android:elevation="0dp"
            android:background="@android:color/transparent"
            app:menu="@menu/bottom_nav_menu" />

    </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:contentDescription="@string/app_name"
        android:src="@drawable/ic_add"
        android:tint="@color/white"
        android:backgroundTint="#FFA500"
        app:layout_anchor="@id/bottomAppBar" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notification"
        android:title="@string/title_notifications" />

    <item
        android:id="@+id/navigation_more"
        android:icon="@drawable/ic_menu"
        android:title="@string/title_more" />

</menu>

And here is the result that I got

enter image description here

But now I want this other icons to be aligned properly according to FAB, So can anyone help me with how to do that?

Upvotes: 1

Views: 733

Answers (1)

mehmet salim ayan
mehmet salim ayan

Reputation: 411

You need to add one empty menu item for fab button like below

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/placeholder"
        android:title="" /> 

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notification"
        android:title="@string/title_notifications" />

    <item
        android:id="@+id/navigation_more"
        android:icon="@drawable/ic_menu"
        android:title="@string/title_more" />

</menu>

Upvotes: 1

Related Questions