When you only need a single floating button, using the FloatingActionButton widget is elegant and neat. If you need multiple floating buttons, using a Stack, Column, or Row widget is a good choice.
The button should be placed in the bottom right corner of the screen. The recommended margin for the bottom is 16dp for phones and 24dp for tablets. In the example above, 16dp was used. The actual drawable size should be 24dp according to the Google design specs.
A floating action button (FAB) is a circular button that triggers the primary action in your app's UI. This page shows you how to add the FAB to your layout, customize some of its appearance, and respond to button taps.
It is a combination of layout gravity and anchor gravity together with playing with the margins (of the anchored item) that can make it work, have a look at the xml below which I have used successfully:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_info" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_marginBottom="0dp"
android:layout_marginEnd="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:src="@android:drawable/ic_media_play"
app:layout_anchor="@id/fab"
app:layout_anchorGravity="top" />
Try to use this app:useCompatPadding="true"
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@android:color/white"
android:src="@mipmap/ic_search"
app:fabSize="normal"
android:scaleType="center"
app:layout_anchor="@+id/bottomSheet"
app:layout_anchorGravity="top|end"
app:useCompatPadding="true"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/accent"
android:src="@mipmap/ic_location_on"
app:backgroundTint="@android:color/white"
app:fabSize="normal"
android:scaleType="center"
android:layout_gravity="top|end"
app:layout_anchor="@+id/fabSearch"
app:layout_anchorGravity="top|end"
android:layout_margin="12dp"/>
Here is my solution, just put an invisible fab between the two fabs, and works well with CoordinatorLayout.
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_check" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_invisible"
android:layout_width="@dimen/fab_margin"
android:layout_height="@dimen/fab_margin"
android:layout_gravity="top|end"
android:layout_margin="@dimen/fab_margin"
android:visibility="invisible"
app:layout_anchor="@id/fab"
app:layout_anchorGravity="top" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_follow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_gps_fixed_follow"
app:backgroundTint="@android:color/white"
app:layout_anchor="@id/fab_invisible"
app:layout_anchorGravity="top" />
No Snackbar
With Snackbar
I can't believe no one has posted the correct answer.
Wrap the buttons in a ViewGroup
and apply the dodgeInsetEdges
layout
parameter so that the buttons move up with the bottom sheet. For the above use
case, we can use a LinearLayout
with the XML attribute
app:layout_dodgeInsetEdges="bottom"
.
Note that we can apply this to any view in a CoordinatorLayout
.
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" ... >
<!-- We can use any ViewGroup here. LinearLayout is the
obvious choice for the questioner's use case. -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:orientation="vertical"
app:layout_dodgeInsetEdges="bottom">
<android.support.design.widget.FloatingActionButton
android:id="@+id/upperFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/fab_margin"
android:src="@drawable/upper_fab_icon" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/lowerFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lower_fab_icon" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
This works for me - FABs are embedded in Relative layout:
<?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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.github.openeet.openeet.SaleDetailActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<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.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="@dimen/fab_margin"
android:layout_gravity="end|bottom" >
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="@android:drawable/ic_menu_share"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/fab_print"
android:layout_alignStart="@+id/fab_print" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_print"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="@android:drawable/ic_media_next"
android:layout_above="@+id/fab_share"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="46dp" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_scrolling"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/big_activity_fab_margin"
android:src="@drawable/ic_share_white_24dp"
app:layout_anchor="@id/app_bar_scrolling"
app:layout_anchorGravity="bottom|end" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_scrolling2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="65dp"
android:src="@drawable/ic_share_white_24dp"
app:layout_anchor="@id/app_bar_scrolling"
app:layout_anchorGravity="bottom|end" />
this is work for me. the key code is android:layout_margin="65dp"
A very simple way (that worked for me!) was to just put the two floating buttons in a vertical linear layout at the bottom/end of its parent.
<LinearLayout
android:layout_gravity="bottom|end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_menu_send" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_menu_send" />
</LinearLayout>
Just adjust the layout_margin attributes for the floating buttons to get the spacing as you want it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With