Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How can we hide/show floating action button(fab) in xml layout

I have a Floating Action Button in my Activity. I am able to show/hide the fab button programmatically using:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.show()
fab.hide()

But I'm unable to set these properties in xml layout. I couldn't find anything to set it hide by default in xml. Here is my xml layout.

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:src="@drawable/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        app:layout_anchor="@id/list_view"
        app:layout_anchorGravity="bottom|right|end" />

UPDATE: I dont want to set my fab button visibility to GONE. I want to set hide() in XML layout. I want to call show() on fab in my activity after a delay.

like image 252
Dileep Perla Avatar asked Dec 18 '15 07:12

Dileep Perla


1 Answers

In "FloatingActionButtonLollipop.java" and "FloatingActionButtonEclairMr1.java", show() checks its visibility first then play animation.

if (mView.getVisibility() != View.VISIBLE || mIsHiding) {
        // If the view is not visible, or is visible and currently being hidden, run
        // the show animation
        mView.clearAnimation();
        mView.setVisibility(View.VISIBLE);
        Animation anim = android.view.animation.AnimationUtils.loadAnimation(
                mView.getContext(), R.anim.design_fab_in);
        anim.setDuration(SHOW_HIDE_ANIM_DURATION);
        anim.setInterpolator(AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR);
        anim.setAnimationListener(new AnimationListenerAdapter() {
            @Override
            public void onAnimationEnd(Animation animation) {
                if (listener != null) {
                    listener.onShown();
                }
            }
        });
        mView.startAnimation(anim);
    } 

I think you can simply set visibility to gone.

like image 172
Owen Hsieh Avatar answered Oct 03 '22 10:10

Owen Hsieh