Search code examples
javaandroidxmlandroid-layoutfloating-action-button

fab.setVisibility(View.GONE) isn't working


I'm not using anchor, it should work. Inicially, the visibility is gone (set by XML). When I press a button, it becomes visible (until here, works). Then, when I press another button, it should becomes gone, but nothing happens. Reduced XML:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/tbTela3"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ToolBar"
        app:titleTextColor="#757575"
        app:subtitleTextColor="#757575" />

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/cLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tbTela3">

        <ScrollView>
            [...]
        </ScrollView>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="16dp"
            android:elevation="8dp"
            android:src="@drawable/replay"
            android:visibility="gone"/>

    </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

Reduced Main Activity:

public void ZoomIn() {

    [...]

    zoomIn.setAnimationListener(new Animation.AnimationListener() {
        public void onAnimationStart(Animation anim) {}
        public void onAnimationRepeat(Animation anim) {}

        public void onAnimationEnd(Animation anim) {
            fab.setVisibility(View.VISIBLE); // WORKS FINE
        }
    });

    fab.startAnimation(zoomIn);
}

[...]

public void Clear() {

    [...]

    anim.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            readX.requestFocus();
            cardII.setVisibility(View.GONE); // WORKS FINE TOO
            fab.setVisibility(View.GONE); // BUT THIS DON'T

            fadeOut.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationStart(Animation anim) {}
                public void onAnimationRepeat(Animation anim) {}

                public void onAnimationEnd(Animation anim) {
                    clear.setVisibility(View.INVISIBLE); 
                }
            });

            clear.startAnimation(fadeOut);
        }
    });

    anim.start();

}

fab.setVisibility(View.GONE) doesn't work independent of where it is on Clear... I reduced the code to be more readable, hope this isn't a problem.


Solution

  •         CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
            p.setAnchorId(View.NO_ID);
            fab.setLayoutParams(p);
            fab.setVisibility(View.GONE);
    

    Or Try

        fab.show(); //show
        fab.hide(); //hide