Search code examples
androidbuttonandroid-activitylockscreenfloating

How to keep floating action button anchored to my view after unlocking the lock screen?


I have a FloatingButton anchored to a view in CoordinatorLayout, all ok, but if I lock the screen and unlock back, the FloatinButton loses its anchored property and stay floating at bottom for a while.

Source code:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.user.myapplication.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <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/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_scrolling" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

As you can see, floatingButton is anchored to app_bar view:

enter image description here

But, if the screen is locked and unlocked back with this activity running, I'm having this for a few seconds:

enter image description here

This issue occurs on Android 4.2 and above,

Some idea?


Solution

  • I found a solution finally, just set the FloatingActionButton´s visibility to GONE in activity´s onPause() method and then set its visibility to VISIBLE in activity´s onResume() method, works nice for this case cause the FloatingActionButton recover its anchored properties when is shown again,

    Source code:

        @Override
        protected void onResume() {
            fab.setVisibility(View.VISIBLE);
            super.onResume();
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            fab.setVisibility(View.GONE);
        }
    

    That´s all,

    Hope it helps everybody with this weird issue.