FloatingActionButton always visible
Asked Answered
F

3

1

I am getting familiar with the new support.design library and I'm facing this issue with the FloatingActionButton. I'm trying to hide it programmatically, but the button stay always visible.

I have the feeling that app:layout_anchor="@id/appbar" must override the visibility of the button.

JAVA:

@InjectView(R.id.search_fab)
FloatingActionButton search_fab;

[...]

search_fab.setVisibility(View.GONE);

XML:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        android:theme="@style/ToolBarStyle"
        >

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_gravity="center"
            android:layout_height="210dp"
            android:id="@+id/top_sales_product_recycler_view"
            />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin"
            android:theme="@style/ToolBarStyle"
            />

    </android.support.design.widget.CollapsingToolbarLayout>


</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/category_recycler_view"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />

<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    app:layout_anchor="@id/appbar"
    app:layout_anchorGravity="bottom|right|end"
    android:src="@drawable/abc_ic_search_api_mtrl_alpha"
    android:layout_margin="@dimen/fab_margin"
    android:id="@+id/search_fab"
    app:borderWidth="1dp"
    />

Feint answered 25/6, 2015 at 13:50 Comment(4)
the id is search_fabShibboleth
that was only due to a wrong copy paste, bug is still the sameFeint
There is currenly a bug with the FAB where visibility is ignored when running below lollipop. code.google.com/p/android/issues/detail?id=175331Portsalut
my issue was different, but it's good to knowFeint
R
4

To get rid of the anchor you can use:

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

As far as I know, having an anchor does really, as you say, affect the possibility of hiding the button, so calling this before the visibility change should work.

Roughage answered 25/6, 2015 at 13:55 Comment(0)
W
0

If you want to show hidden FAB

   <android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">


    <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:fitsSystemWindows="true">
    ...

    </android.support.design.widget.AppBarLayout>

    ...

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:visibility="gone"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:clickable="true"/>

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

and

 CoordinatorLayout.LayoutParams p = new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.WRAP_CONTENT, CoordinatorLayout.LayoutParams.WRAP_CONTENT);
    p.anchorGravity = Gravity.BOTTOM | Gravity.END;
    p.setAnchorId(R.id.appbar);
    p.setMargins(...);
    fab.setLayoutParams(p);
    fab.setVisibility(View.VISIBLE);
Wavelet answered 27/11, 2015 at 21:15 Comment(0)
S
0

fab.setVisibility(View.INVISIBLE); Wasn't working.

Using : fab.hide(); solved the problem.

Sazerac answered 9/3, 2016 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.