How to define individual toolbar for different destination's on Naviagtion drawer with Navigation component

18 views Asked by At

I know this question has on answer on developer site **Support app bar varients **But they doesn't show the proper implementation of that using Navigation Component for app destination is simple and efficient i already use Navigation component on Bottom Navigation View on bottom navigation is very simple just setUpWithNavController and it also quite easy on tab layout, but i case of Navigation drawer that quite hard and when we also make some customization it really painful Ex: i use Navigation drawer along with 4 some fragment i want to use Custom toolbar for Different fragment destination that is not possible for default implementation When we define Toolbar in MainActivity with setSupportActionBar(TOOLBAR) and use **SetUpActionBarWithNavController(navController,appBarConfiguration) **

It uses same Toolbar for all other Destination with some label name changes only like i show that in this gifenter image description here

As per Official documentation when we want to support app bar varients we need to define custom toolbar for individual fragment and remove the toolbar in MainActivity WHEN WE REMOVE THE TOOLBAR IN MAINACTIVITY and IF WE USE NoActionBar in themes WE also doesn't use this line " setupActionBarWithNavController(navController, appBarConfiguration) " So i defined individual toolbar for different fragment like this

fragments_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.gallery.GalleryFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/appBarlayout_Gallety">


        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            app:title="Gallery"
            app:menu="@menu/custom_menu_gallery"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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


</RelativeLayout>  

and i modified my MainActivity code like this (remember i set NoActionBar attribute in Themes.xml):

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private var binding: ActivityMainBinding? = null
    private val _binding: ActivityMainBinding
        get() = binding!!

    private lateinit var navController: NavController
    private lateinit var navHostFragment: NavHostFragment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment_content_main) as NavHostFragment
        navController = navHostFragment.navController
        
        appBarConfiguration = AppBarConfiguration(  
            setOf(
                R.id.nav_home, R.id.profileFragment,
                R.id.nav_slideshow, R.id.nav_gallery
            ), _binding.mainDrawerLayout
        )

       // setupActionBarWithNavController(navController, appBarConfiguration)    i doesn't use this because i didn't
        // set custom toolbar and also i defined NoActionBar in my themes
        _binding.navView.setupWithNavController(navController = navController)

    }
    
    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }

    override fun onDestroy() {
        binding = null
        super.onDestroy()
    }
}

When i run this in my toolbar "hamberger" icon or drawer icon doesn't appear when i manualy swipe left to right it open Navigation View when i click it open appropriate fragment like this :enter image description here

I want Behaviour like different Custom toolbar for different fragment

** if my approach is wrong how can i archive that.

** if my implementation is correct what i do next to archive the functionality.

0

There are 0 answers