8

I have to change FAB state in UtteranceProgressListener callback methods but setImageResource() does nothing when called after setBackgroundTintColorList(). However it does change src image when I comment out setBackgroundTintColorList().

setBackgroundTintColorList();
setImageResource();
//doesn't work

//setBackgroundTintColorList();
setImageResource();
//Now it works.

What's wrong.

2 Answers 2

14

I've encountered the same problem - I'm guessing it's a bug. Even though my Fab is visible, call hide() and show() before and after the set, as a workaround.

fun FloatingActionButton.changeFab(@ColorRes colorRes: Int, @DrawableRes imageRes: Int) {
    hide()
    backgroundTintList = ContextCompat.getColorStateList(context, colorRes)
    setImageResource(imageRes)
    show()
}

Bug report: https://issuetracker.google.com/issues/111316656

2

Just an addendum:
In my program the FAB is part of a collapsing toolbar. Changing the icon used to fail after some successful attempts, ending in an empty FAB, maybe due to a race condition bug or state bug in Google's library. I first removed the setBackgroundTintList(), but contrary to the given example this did not work, i.e. the FAB remained coloured, but empty. So I finally applied ersin-ertan's great workaround, but in Java, what looks like this:

    mFloatingButton.hide();
    mFloatingButton.setImageResource(...);
    mFloatingButton.setBackgroundTintList(...);
    mFloatingButton.show();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.