MrShadow
MrShadow

Reputation: 171

FloatingActionButton visible for sometime even if visibility is set to gone

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?

Upvotes: 2

Views: 4765

Answers (3)

Arjun
Arjun

Reputation: 59

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);

Upvotes: 0

Karthik kai
Karthik kai

Reputation: 141

        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);

Upvotes: 5

Kezufru
Kezufru

Reputation: 123

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

Upvotes: 0

Related Questions