how to change toolbar icon (hamburger icon) when using navigation drawer with jetpack navigation component
U

1

5

I'm using jetpack navigation component to build a navigation drawer, I want to change hamburger icon of the toolbar, I tried many solutions like bellow but they don't work

app:navigationIcon="@drawable/menu"

also

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.menu);

and this is my code

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)
    navController=findNavController(R.id.nav_host_fragment)
    appBarConfiguration=AppBarConfiguration(setOf(R.id.nav_acceuil,R.id.nav_notifications,R.id.nav_gerer,R.id.nav_deconnexion),drawer_layout)
    setupActionBarWithNavController(navController,appBarConfiguration)
    nav_view.setupWithNavController(navController);
}
Unthinking answered 2/8, 2020 at 14:49 Comment(0)
F
6

You can use the addOnDestinationChangedListener:

    navController.addOnDestinationChangedListener { controller, destination, arguments ->
        if (destination.id == R.id.nav_xxxx){
            supportActionBar?.setHomeAsUpIndicator(R.drawable.xxxx)
        }
    }
Flamenco answered 2/8, 2020 at 15:2 Comment(3)
FINALLY WORKS THAAANKSUnthinking
While this magically works, it doesn't make sense, semantically. I'm from web world and i would understand if this implicitly is "DOM ready" kinda of trick in android world. Is it?Archenteron
It's work for me, but why if we put this on onCreate() it's doesn't work?Doubloon

© 2022 - 2024 — McMap. All rights reserved.