I am trying to hide my FloatingActionButton fabLocation
programmatically with :
fabLocation.setVisibility(View.GONE)
but it does not work.
If I add android:visibility="gone"
in my XML layout, fabLocation
is hidden when I run my activity but it reappears when I scroll.
Here is my layout:
<?xml version="1.0" encoding="utf-8"?> <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/main_content" 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:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="@color/colorOverlay" app:expandedTitleMarginEnd="64dp" app:expandedTitleMarginStart="48dp" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:id="@+id/img_couverture" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:scaleType="centerCrop" android:src="@drawable/bg_navigation_drawer_header" app:layout_collapseMode="parallax" /> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.CardView android:layout_marginTop="8dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" /> <View android:background="@drawable/separator_orange_gradient" android:layout_marginTop="8dp" android:layout_marginBottom="16dp" android:layout_width="match_parent" android:layout_height="2dp"/> <TextView android:id="@+id/tv_history" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="@style/TextAppearance.RobotoLight" /> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout> </android.support.v4.widget.NestedScrollView> <android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" android:clickable="true" android:id="@+id/fab_location" android:src="@drawable/ic_fab_location_24dp" app:backgroundTint="@color/colorOrange" app:layout_anchor="@id/appbar" app:layout_anchorGravity="bottom|right|end" />
It is due to the app:layout_anchor
attribute. You must get rid of the anchor before changing visibility:
CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); p.setAnchorId(View.NO_ID); fab.setLayoutParams(p); fab.setVisibility(View.GONE);
FloatingActionButtons anchored to AppBarLayouts have a special relationship where their visibility is controlled by the scroll position of the AppBarLayout.
You can turn this off via the behavior_autoHide
attribute:
<android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_anchor="..." app:behavior_autoHide="false"/>
You can also do this programmatically if you wish:
EDIT:
fab.getBehavior() was not correct, use getBehavior()
from its LayoutParams
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); FloatingActionButton.Behavior behavior = (FloatingActionButton.Behavior) lp.getBehavior(); if (behavior != null) { behavior.setAutoHideEnabled(false); } else { behavior = new FloatingActionButton.Behavior(); behavior.setAutoHideEnabled(false); lp.setBehavior(behavior); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With