user924
user924

Reputation: 12293

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

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

Upvotes: 2

Views: 740

Answers (2)

Pavel
Pavel

Reputation: 33

For anyone who is reading this and searching the answer, ust add this to your custom toolbar in xml:

    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

Upvotes: 2

Ecclesiaste Panda
Ecclesiaste Panda

Reputation: 427

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!

Upvotes: 1

Related Questions