I can't hide my FloatingActionButton
. Here is my code:
XML:
<CoordinatorLayout>
<AppBarLayout android:id="@+id/appbar">
<CollapsingToolbarLayout>
<ImageView/>
<android.support.v7.widget.Toolbar />
</CollapsingToolbarLayout>
</AppBarLayout>
<NestedScrollView />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|right|end"/>
</CoordinatorLayout>
And I'm calling:
fab.clearAnimation();
fab.setVisibility(View.GONE);
I'm trying to hide the FAB, but it seems that setVisibility
+ clearAnimation
does not work if the FAB is in a CoordinatorLayout
.
Even if I call fab.clearAnimation
, the animation is still triggered. Can anyone help me?
I ran into exactly the same issue. It would seem that Google's Android team doesn't want you to have control of the visibility when the FloatingActionButton is anchored to an AppBarLayout, as discussed in this issue - FloatingActionButton Ignores View Visibility
It looks like a viable fix is wrapping the FAB in a FrameLayout and setting the visibility on the wrapper instead, like this:
You may however wish to consider whether this is the ideal behaviour. Google advocates recommend that the FAB be visible as soon as the screen is created. If you're hiding it for longer than required to animate it in, consider showing a disabled state instead.
Use show and hide methods to show and hide the Floating Action Button
If your issue is the animation, you could try invalidating the FAB
Behavior
. As for the visibility, you should null the anchor you have set in your layout:you can disable it and make it semitransparent like this
It works for me,
setVisibility
doesn't work forFloatingActionButton
as it belongs to anotherviewGroup
that doesn't havesetVisibility
method.