0

I want to have a BottomNavigationView with a cradled FAB in the center. I wrapped my BottomNavigatonView inside a BottomAppBar.

<?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=".ui.home.HomeActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:backgroundTint="@color/toolbarAndBottomBarColor"
            app:contentInsetStart="0dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:backgroundTint="@android:color/transparent">

                <ImageView
                    android:id="@+id/imageViewDrawerMenu"
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:layout_marginStart="16dp"
                    android:backgroundTint="@android:color/transparent"
                    android:src="@drawable/ic_drawer_menu"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:tint="@color/primaryYellow" />

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/textViewTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_marginStart="8dp"
                    android:backgroundTint="@android:color/transparent"
                    android:gravity="center"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.5"
                    app:layout_constraintStart_toEndOf="@id/imageViewDrawerMenu"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:text="Home" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </com.google.android.material.appbar.MaterialToolbar>

    </com.google.android.material.appbar.AppBarLayout>

    <include
        layout="@layout/content_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_marginBottom="?attr/actionBarSize" />

    <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:backgroundTint="@color/toolbarAndBottomBarColor"
        app:contentInsetEnd="0dp"
        app:contentInsetStart="0dp"
        app:fabAlignmentMode="center"
        app:fabCradleMargin="10dp"
        app:fabCradleRoundedCornerRadius="16dp">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:itemIconSize="32dp"
            app:itemIconTint="@color/primaryYellow"
            app:labelVisibilityMode="unlabeled"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/navigation_bottom_menu" />

    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add"
        app:backgroundTint="@color/toolbarAndBottomBarColor"
        app:layout_anchor="@id/bottomAppBar"
        app:tint="@color/primaryYellow" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

However I am unable to see the cradle in the final app. No cradle

I have even set the bottomNavigationView background to null in the Activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.appedia.birthdays.R
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.toolbar_home.*

class HomeActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        bottomNavigationView.background = null
    }
}

Changing the android:background property of the BottomNavigationView to any other color does take effect. But the area around the icons still remains black. It changes if I update the value of itemBackground. However I tried setting this property to @android:color/transparent but it still does not work.

Pratik Banodkar
  • 105
  • 1
  • 10

1 Answers1

0

To show FAB Cradle :

You need to add backgroundColor to transparent and the elevation to 0dp to the BottomNavigationView.

<com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottomNavigationView"
                ...
                android:background="@android:color/transparent"
                app:elevation="0dp"
                ... />

To change background color of BottomNavigationView :

You require to create one drawable_selector.xml file having android:state_checked .

Make sure to use android:drawable in the selector file.

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

Use that drawable with app:itemBackground of BottomNavigationView

<com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            ...
            app:itemBackground="@drawable/drawable_selector"
            ... />
Nikunj
  • 3,937
  • 19
  • 33