Reputation: 9198
I am using Navigation Component.
I have Toolbar
inside AppBarLayout
but I have to hide toolbar in a specific fragment. For this purpose I have used onDestinationChanged
from Navigation component to listen route and hide but toolbar never hides.
override fun onDestinationChanged(
controller: NavController, destination: NavDestination,
arguments: Bundle?
) {
if (destination.id == R.id.helpFragment) {
Timber.e("App Bar Hide")
binding.appBarLayout.setExpanded(false, true) //This never hides toolbar
} else {
Timber.e("App Bar Show")
binding.appBarLayout.setExpanded(true, true)
}
}
Here is my layout
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
app:elevation="0dp">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="@string/home" />
</com.google.android.material.appbar.AppBarLayout>
Upvotes: 1
Views: 571
Reputation: 21
val navHostFragment: NavHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController: NavController = navHostFragment.navController
val appBarConfiguration: AppBarConfiguration = AppBarConfiguration(navController.graph)
binding.toolbar.setupWithNavController(navController, appBarConfiguration)
setSupportActionBar(binding.toolbar)
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.salesFragment -> {
supportActionBar?.hide() // to hide
} else -> {
supportActionBar?.show() // to show
}
}
}
Upvotes: 2