codeMan
codeMan

Reputation: 75

How to hide the Floating Action Button from Bottom App Bar Layout

I'm creating a Bottom App Bar with an Floating Action Button. The App Bar is part of my Main Activity and I want to hide the FAB for some Fragments within the Activity.

I already tried 'View.GONE' and 'fab.hide()'. Then I tried to hide the Fab with following function:

    private fun hideFloatingActionButton() {
        val params = fab.layoutParams as CoordinatorLayout.LayoutParams
        val behavior = params.behavior as FloatingActionButton.Behavior?

        if (behavior != null) {
            behavior.isAutoHideEnabled = false
        }

        params.anchorId = View.NO_ID;
        params.width = 0;
        params.height = 0;
        fab.layoutParams = params
        fab.hide()
    }

My layout.xml:

    <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"
        tools:context=".MainActivity"
        android:background="@color/backPrimary"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    ...

         content
    ...

    <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            style="@style/Widget.MaterialComponents.BottomAppBar"
            app:navigationIcon="@drawable/ic_burger_menu"
            app:fabAlignmentMode="end"
    />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/start_project_day"
            app:srcCompat="@drawable/ic_next"
            app:layout_anchor="@id/bar"
    />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Upvotes: 0

Views: 2387

Answers (1)

Catluc
Catluc

Reputation: 1823

Just tested out with fab.hide() method and it works. The "logic" to hide the fab should occur in the activity and not in the fragment. The next logic is set in the activity and the hide part is inside the else branch at the end.

navController.addOnDestinationChangedListener { controller, destination, _ ->

        bar.animate().translationY(0f)


        // First page is main menu
        if(controller.graph.startDestination == destination.id){


            bar.navigationIcon = icon
            bar.fabAlignmentMode = BottomAppBar.FAB_ALIGNMENT_MODE_CENTER
            fab?.setImageDrawable(getDrawable(R.drawable.ic_local_wtf))

        }else{

            // Hide navigation drawer icon
            bar.navigationIcon = null

            // Move FAB from the center of BottomAppBar to the end of it
            bar.fabAlignmentMode = BottomAppBar.FAB_ALIGNMENT_MODE_END



            // Replace the action menu
            //bar.replaceMenu(bottomappbar_menu_secondary)
            invalidateOptionsMenu()

            // Change FAB icon
            fab?.setImageDrawable(getDrawable(R.drawable.ic_reply_white_24dp))
            fab.hide()
        }

    }

Upvotes: 1

Related Questions