I have MainActivity with FragmentContainerView, BottomNavigationView and FAB.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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:id="@+id/motionLayoutActivityMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_main_scene"
tools:context=".ui.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.fragment.app.FragmentContainerView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="selected"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation_menu" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/verticalGuidelineActivityMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_percent="0.8" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/filterFabActivityMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="72dp"
android:contentDescription="fab"
android:src="@android:drawable/ic_menu_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/verticalGuidelineActivityMain" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/horizontalGuidelineActivityMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="1"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.motion.widget.MotionLayout>
When I click on item1 or item2 of BottomNavifationView called fab.show(), if i click on item3 called fab.hide().
MainActivity.kt:
//SOME CODE BEFORE
binding.bottomNavigation.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.membersFragment -> {
fragmentManager.beginTransaction().hide(activeFragment).show(membersFragment).commit()
activeFragment = membersFragment
// Show fab when select membersFragment
binding.filterFabActivityMain.show()
true
}
R.id.teamsFragment -> {
fragmentManager.beginTransaction().hide(activeFragment).show(teamsFragment).commit()
activeFragment = teamsFragment
// Show fab when select teamsFragment
binding.filterFabActivityMain.show()
true
}
R.id.aboutFragment -> {
fragmentManager.beginTransaction().hide(activeFragment).show(aboutFragment).commit()
activeFragment = aboutFragment
// Hide fab when select aboutFragment
binding.filterFabActivityMain.hide()
true
}
else -> false
}
}
//SOME CODE AFTER
fab.hide() hides FAB, but if I click on FAB position fab.clickListener will be called.
fab.show() will not show FAB.
MotionLayout controls scene, so need to use app:visibilityMode="ignore"
for Constraint of FAB in motion scene.
<Constraint
android:id="@+id/filterFabActivityMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="72dp"
android:contentDescription="fab"
android:src="@android:drawable/ic_menu_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/verticalGuidelineActivityMain"
app:visibilityMode="ignore" />