Floatingactionbutton Setvisibility() Not Working
Solution 1:
Use show and hide methods to show and hide the Floating Action Button
FloatingActionButtonfab= (FloatingActionButton) findViewById(R.id.fab);
// To show the Floating Action Button
fab.show();
// To hide the Floating Action Button
fab.hide();
Solution 2:
For FloatingActionButton
the setVisibility()
method will give you an error while building with the latest Gradle 6.x
and build-tool 28.x.x
and is not encouraged any more. Instead, use:
fab.hide() // fab.setVisibility(View.GONE)
fab.show() // fab.setVisibility(View.VISIBLE)
Note: Succesfully tested on
Android Studio 3.4.1
,Gradle 6.0
andbuild-tool 28.0.1
Solution 3:
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:
CoordinatorLayout.LayoutParamsp= (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setBehavior(null); //should disable default animations
p.setAnchorId(View.NO_ID); //should let you set visibility
fab.setLayoutParams(p);
fab.setVisibility(View.GONE); // View.INVISIBLE might also be worth trying//to bring things back to normal state
CoordinatorLayout.LayoutParamsp= (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setBehavior(newFloatingActionButton.Behavior());
p.setAnchorId(R.id.appbar);
fab.setLayoutParams(p);
Solution 4:
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:
<android.support.design.widget.FrameLayout
android:id="@+id/fab_container"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="invisible">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fab_icon"/>
</android.support.design.widget.FrameLayout>
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.
Solution 5:
you can disable it and make it semitransparent like this
fab.setEnabled(false);
fab.setClickable(false);
fab.setAlpha(0.3f);
Post a Comment for "Floatingactionbutton Setvisibility() Not Working"