2
votes

In the Google's Material Design specs they often shown the Floating Action Button lying over half the toolbar and have over the content. image of the design

http://www.google.com/design/spec/components/buttons-floating-action-button.html

But I have tried a few variations and there is still a gap between the toolbar and content, caused by the button.

<LinearLayout>
 <include layout="@layout/toolbar" />   

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

  <ScrollView>
    Content
  </ScrollView>
</LinearLayout>

I have also tried placing both toolbar and FAB button in a FrameLayout and it also had the gap. The FAB button code was taken from Google's samples, and I haven't had issues with having it overlap at the bottom of over a RecyclerView.

Is there a way to achieve this look shown in the Material Design Specs.

2

2 Answers

2
votes

Add layout_anchor property in FAB and set it to Top View.

Make CoordinatorLayout as your root view, this would be best layout practice.

You can set FAB gravity using layout_anchorGravity attribute in FAB.

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <LinearLayout
            android:id="@+id/viewA"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.6"
            android:background="@android:color/holo_blue_bright"
            android:orientation="horizontal"/>

        <LinearLayout
            android:id="@+id/viewB"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.4"
            android:background="@android:color/holo_green_light"
            android:orientation="horizontal"/>

    </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:clickable="true"
        android:src="@drawable/ic_done"
        app:layout_anchor="@id/viewA"
        app:layout_anchorGravity="bottom|right|end"/>
    </android.support.design.widget.CoordinatorLayout>

Check this out. Hope this could help you.

1
votes

Using Relative layout is the easiest to position a FAB in between 2 views. You can use elevation parameter for the fab to get it over the toolbar. Set the elevation of the FAB more that that of the toolbar.

<RelativeLayout 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"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <ListView
        android:id="@+id/listview"
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


    <com.getbase.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/favorite"
        android:layout_alignBottom="@+id/toolbar"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:elevation="8dp"
        android:layout_marginBottom="-32dp"
        fab:fab_icon="@drawable/ic_favorite_outline_white_24dp"
        fab:fab_colorNormal="@color/accent"
        fab:fab_size="mini"
        />
</RelativeLayout>