Changing color of back arrow icon of toolbar dynamically when using Navigation Component
Asked Answered
M

2

2

So I have single activity app with single toolbar placed in layout of activity.

The next code only works just to change color of navigation icon button if there is no fragment was inflated yet

binding.toolbar.setNavigationIconTint(ContextCompat.getColor(this, R.color.white))

But as soon as navigation component opens any of the fragment with arrow back button (if it can go back to previous fragment) then color of arrow icon of back button is different (it's black)

Even the following code doesn't help to change color of arrow icon of back button:

override fun onDestinationChanged(
    controller: NavController,
    destination: NavDestination,
    arguments: Bundle?
) {
   binding.toolbar.setNavigationIconTint(ContextCompat.getColor(this, R.color.white))

}

It's still black arrow

Why I can't change it when using fragments with NavigationComponent and why it sets to some back color (default one or what)?

The navigation icon is being set by NavigationComponent. It can be arrow back or it can be menu (hamburger) icon if current fragment is one of fragments set for AppBarConfiguration(fragments)

When I set it like this in onDestinationChanged then it will be changed, but here I manually set icon and color, and I have to add logic if I should set arrow or menu icon, so it complicates everything and it's boilerplate code, because NavigationCompopnent can handle it itself:

binding.toolbar.setNavigationIcon(R.drawable.ic_arrow) // or menu (need to add logic which icon should be used)
binding.toolbar.setNavigationIconTint(ContextCompat.getColor(this, R.color.white))

All I want is just to change color of navigation icon but not icon itself.

My app just can have different toolbar style (transparent or solid) based on current fragment. That's why I want to change icon color dynamically

Mccue answered 15/8, 2022 at 14:2 Comment(0)
H
2

For anyone who is reading this and searching the answer. Just add this to your custom toolbar in xml

    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
Headrail answered 18/2, 2023 at 21:7 Comment(0)
E
1

This worked for me

To effectively set the tint color of the navigation icon programmatically you need to set the drawable first and apply the tint afterwards.

First create a doJob method from your activity

fun doJob() {
    binding.toolbar.title = "Reached"
    binding.toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_24)
    binding.toolbar.children.forEach {
        (it as? AppCompatImageButton)?.imageTintList =
            ColorStateList.valueOf(Color.GREEN)
        it.refreshDrawableState()
    }
}

then on your fragment once the onViewCreated() has been called you can call your doJob method of the activity

In Fragment

((requireActivity()) as MainActivity).doJob()

Et voila!

Entertaining answered 15/8, 2022 at 15:2 Comment(2)
No, thanks, looks like a bad code. Also here you set a specific navigation icon, but it can be arrow or menu icon (if you have drawer) and navigation component automatically sets this icon, you don't have to set it manuallyMccue
Did you try this code and found thad it didn't work with the menu icon in case of Nav drawer? and if by menu icon you mean the burger icon of the nav drawer this is working correctly unless you have more details of how this is a bad code and going against the NavComponent rules. thanksEntertaining

© 2022 - 2024 — McMap. All rights reserved.