Dileep Perla
Dileep Perla

Reputation: 1865

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.

Upvotes: 5

Views: 19076

Answers (4)

CSA
CSA

Reputation: 29

  android:visibility="invisible"

I tried this on xml and simply called fab.show() It works with animation in my device. (Andoird 5.1.1)

Upvotes: 2

Shshank Bhong
Shshank Bhong

Reputation: 257

Simple way will be add FloatingActionButton in RelativeLayout or LinearLayout and use GONE/VISIBLE action to container layout.

Upvotes: 2

ekar
ekar

Reputation: 137

To have FloatingActionButton show() call animated effect you need to use visibility="invisible", so the button's view is laid out in the layout.

See the implementation of the show method in support library:

void show(@Nullable final InternalVisibilityChangedListener listener, final boolean fromUser) {
        if (isOrWillBeShown()) {
            // We either are or will soon be visible, skip the call
            return;
        }

        mView.animate().cancel();

        if (shouldAnimateVisibilityChange()) {
            mAnimState = ANIM_STATE_SHOWING;
...

and the method

private boolean shouldAnimateVisibilityChange() {
    return ViewCompat.isLaidOut(mView) && !mView.isInEditMode();
}

Upvotes: 2

Owen Hsieh
Owen Hsieh

Reputation: 139

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.

Upvotes: 5

Related Questions