Samuel Robert
Samuel Robert

Reputation: 11072

How to prevent FAB from scrolling with CoordinateLayout in Android

I've the below layout xml for my activity

<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:background="@color/white"
     android:fitsSystemWindows="true"
     tools:context=".HomeActivity">

     <android.support.design.widget.AppBarLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/AppTheme.Dark.AppBarOverlay"
          app:elevation="0dp">

          <android.support.v7.widget.Toolbar
               android:id="@+id/toolbar"
               android:layout_width="match_parent"
               android:layout_height="?attr/actionBarSize"
               android:background="?attr/colorPrimary"
               app:layout_scrollFlags="scroll|enterAlways"
               app:popupTheme="@style/AppTheme.Dark.PopupOverlay">
     </android.support.design.widget.AppBarLayout>

     <RelativeLayout
        android:id="@+id/rl_updates"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/ll_footer"
        android:layout_below="@+id/ll_fibres_search">

        <android.support.v4.widget.SwipeRefreshLayout
              android:id="@+id/srl_updates"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

               <android.support.v7.widget.RecyclerView
                     android:id="@+id/rv_all_requirements"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:divider="@color/iron"
                     android:dividerHeight="1px"/>
         </android.support.v4.widget.SwipeRefreshLayout>

         <android.support.design.widget.FloatingActionButton
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_alignParentEnd="true"
              android:layout_alignParentRight="true"
              android:layout_marginBottom="@dimen/activity_horizontal_margin"
              android:layout_marginEnd="@dimen/activity_horizontal_margin"
              android:layout_marginRight="@dimen/activity_horizontal_margin"
              android:onClick="onRequirement"
              android:overScrollMode="never"
              android:src="@drawable/ic_playlist_add_white_24dp"
              app:backgroundTint="@color/pink"/>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

When I scroll the Recycler View, the App Bar collapses just like I wanted it to be. But the problem is the FAB also shifts down a little bit. I set 16dp margin to the bottom but it sticks to the bottom of the screen.

How do I prevent the FAB from shifting down initially?

This is how it looks now enter image description here

Upvotes: 3

Views: 1498

Answers (3)

MohanadMohie
MohanadMohie

Reputation: 1044

Have you tried taking the FloatingActionButton outside the RelativeLayout and inside the CoordinatorLayout? You can do that and give it the attribute:

    android:layout_gravity="bottom|right|end"

instead of:

    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"

Upvotes: 2

sivaBE35
sivaBE35

Reputation: 1891

Replace RelativeLayout with FrameLayout and add this line to your FAB

android:layout_gravity="right|bottom"

like this

<FrameLayout 
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/srl_updates"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_all_requirements"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
 android:divider="@color/iron"
            android:dividerHeight="1px"/>
    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_horizontal_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:onClick="onRequirement"
        android:layout_gravity="right|bottom"
        android:overScrollMode="never"
        android:src="@drawable/ic_playlist_add_white_24dp"
        app:backgroundTint="@color/pink"/>

</FrameLayout>

Upvotes: 0

Sam
Sam

Reputation: 45

set anchor for your FAB button

app:layout_anchorGravity="bottom|right|end" android:layout_gravity="right"

Upvotes: 0

Related Questions