2

I am having a FAB (FloatingActionButton) in my LinearLayout. I want the LinearLayout to be hidden i.e. visibility set to gone at first and on clicking menu button it should get visible. Everything is working fine but FAB remains visible for sometime before hiding itself. I tried everything from changing visibility of FAB and using hide() and show() but nothing works.

I got the following links but for some reason they don't work for me.

  1. FloatingActionButton always visible
  2. Gone FAB becames visible for a while
  3. FloatingActionButton doesn't hide

My activity_main.xml:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.wolfarts.checklist2.MainActivity">

    <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="0dp"
        android:paddingTop="24dp"
        android:theme="@style/MyToolbarStyle">

            <Spinner
                android:id="@+id/spinner_nav"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                style="@style/Base.TextAppearance.AppCompat.Title"
                android:visibility="gone"/>

    </android.support.v7.widget.Toolbar>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/emptyStateView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"   
            android:orientation="vertical"
            android:gravity="center">

           <ImageView
                android:src="@drawable/emptystatebag"
                android:layout_marginTop="-30dp"
                android:layout_gravity = "center"
                android:layout_height="170dp"
                android:layout_width="300dp"/>

           <TextView
                android:paddingTop="8dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:gravity = "center"
                android:text="Nothing here. Try Adding Something."
                android:textColor="#B8B8B8"
                android:textSize="18sp"/>

           </LinearLayout>

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

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:animateLayoutChanges="true">

                        <LinearLayout
                            android:id="@+id/toolbarContent"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical"
                            android:paddingLeft="72dp"
                            android:paddingStart="72dp"
                            android:paddingRight="18dp"
                            android:paddingEnd="18dp"
                            android:paddingBottom="80dp"
                            android:visibility="gone"
                            android:elevation="4dp"
                            android:descendantFocusability="beforeDescendants"
                            android:focusableInTouchMode="true">

                            <android.support.design.widget.TextInputLayout
                                android:id="@+id/username_text_input_layout"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:textColorHint="#B3FFFFFF"
                                android:layout_gravity="center_vertical">

                                <EditText
                                    android:id="@+id/taskName"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:textSize="35sp"
                                    android:maxLines="1"
                                    android:hint="Item Name"/>

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

                        </LinearLayout>

                        <com.wolfarts.checklist2.CustomRecyclerView
                            android:id="@+id/taskListRecyclerView"
                            android:layout_height="match_parent"
                            android:layout_width="match_parent"/>

            </LinearLayout>

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:paddingLeft="16dp"
                android:clickable="true"
                android:src="@drawable/ic_done_white_24dp"
                app:layout_anchor="@id/toolbarContent"
                app:layout_anchorGravity="bottom|right|end"
                app:backgroundTint="@color/orange"
                android:visibility="gone"
                app:elevation="8dp"/>

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

    </FrameLayout>

</LinearLayout>



<LinearLayout
    android:id="@+id/navDrawer"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="left|start">

    <ListView
        android:id="@+id/navList"
        android:paddingTop="24dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#ffeeeeee"/>

</LinearLayout>

Is this a bug in FAB?

Community
  • 1
  • 1
MrShadow
  • 171
  • 3
  • 16
  • may be you are changing it dynamically check your code again, usually `android:visibility="gone"` works – Hemant Shori Sep 23 '15 at 06:58
  • @HemantShori I want it to dynamically change its visiblity on button press i.e. onClick. – MrShadow Sep 23 '15 at 07:01
  • what do you mean u have tried everything from changing visibility of FAB and using hide() and show() but nothing works. use should use `fab.setVisibility(View.VISIBLE);` or if you want to do it in backpress Override the method and call this – Hemant Shori Sep 23 '15 at 07:04
  • @HemantShori I tried fab.setVisibility(View.GONE); and fab.hide(); to make it hidden as well as fab.setVisibility(View.VISIBLE); and fab.show(); to make it visible. My problem is it remains visible for a fraction of second and then hides it self. I want it to be hidden from the time I start the app and become visible only when I trigger a event i.e. button click – MrShadow Sep 23 '15 at 07:09

3 Answers3

5
        CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams)    fab.getLayoutParams();
        p.setAnchorId(View.NO_ID);
        p.width = 0;
        p.height = 0;
        fab.setLayoutParams(p);
        fab.setVisibility(View.GONE);
Karthik kai
  • 141
  • 6
0

Have you tried this?: FloatingActionButton doesn't hide Removing the anchor seems to affect the setVisibility of the FAB.

Community
  • 1
  • 1
Kezufru
  • 123
  • 10
0

I was facing the same issue , floating action button shows up for sometime before hiding . First thing you should do is remove the anchor gravity , anchor and set its height and width to 0 in layout.

When you want to make it visible , set the height ,width , anchor , anchor gravity programitically like this :

  CoordinatorLayout.LayoutParams p = new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

   p.setAnchorId(R.id.appbar);
   p.anchorGravity = Gravity.BOTTOM|Gravity.END|Gravity.RIGHT;
   fab.setLayoutParams(p);
   fab.setVisibility(View.VISIBLE);
Arjun
  • 59
  • 1
  • 8