OmriSoudry OmriSoudry - 6 months ago 83
Android Question

android floating action button show() not working

I have a floating action button (FAB) and an Async Task that calls FAB.hide() before loading some data in background, and after it's done. For some reason, Even though my console log clearly shows a call is always being made to hide() and then show(), sometimes the FAB hides but doesn't show until the task is executed again.


The problem seems to happen if the calls are too close. I actually tried the code:


and the same problem occurs (fab is not showing).
Any ideas for handling this?


I came across a similar situation. The problem seems to be that the floating action button doesn't consider itself shown or hidden until it's show/hide animation is finished.

So if you call hide(); but then call show(); before the hide animation has finished, then the show animation won't run because the button is still set as being in a shown state already.

I don't know if this is the optimum solution but I solved it as follows:

boolean fabShouldBeShown;
FloatingActionButton.OnVisibilityChangedListener fabListener = new FloatingActionButton.OnVisibilityChangedListener() {
    public void onShown(FloatingActionButton fab) {

    public void onHidden(FloatingActionButton fab) {

public void methodWhereFabIsHidden() {
    fabShouldBeShown = false;

public void methodWhereFabIsShown() {
    fabShouldBeShown = true;;

By setting our own boolean along with this listener we can deal with these interrupted situations. When we get to the end of the animation we check if we are in the state that we actually want to be, and if not we change to the correct one.