I am creating a simple app using material design. I want to get two floating buttons inside one layout.
And I want to make them move together when a snack bar is being displayed.
But I can't do this correctly because the layout_margin
doesn't work.
These are the screenshots of the app and the markup. Can you help me?
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/addProductsContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/addProductsAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="@+id/addProductsTabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/addProductsViewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/addProductFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:src="@drawable/ic_add_white_36dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/searchFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/addProductFab"
app:layout_anchorGravity="top|right|end"
android:layout_marginBottom="80dp"
android:layout_marginRight="16dp"
android:src="@drawable/ic_search_white_36dp" />
</android.support.design.widget.CoordinatorLayout>
Don'tDon't display multiple FABs on a single screen. CautionOccasionally two FABs can be used, if they perform distinct, yet equally important, actions.
Consider a code snippet where Scaffold Widget takes a two floating action button. You must use SpeedDial Class and on children[] you can add some buttons with SpeedDialChild. The sample below shows 2 FABs.
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.
I tried some tweaks with your code to get it working and in the process I have gained some understanding about the working of anchors.
First thing to notice is that the CoordinatorLayout aligns its child views based on the view's borders. So adding margin between elements wouldn't help at all in motion. It will look fine in the layout, but give up in motion.
So adding a dummy view helps in anchoring properly without issues. Also, you need to appropriately set the layout_gravity
attributes.
Just replace the code for the two FloatingActionButtons in your layout with this snippet:
<android.support.design.widget.FloatingActionButton
android:id="@+id/addProductFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:src="@drawable/ic_add_white_36dp" />
<View
android:id="@+id/dummy"
android:layout_width="1dp"
android:layout_height="16dp"
app:layout_anchor="@id/addProductFab"
app:layout_anchorGravity="top|right|end" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/searchFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|top"
android:layout_margin="16dp"
android:src="@drawable/ic_search_white_36dp"
app:layout_anchor="@id/dummy"
app:layout_anchorGravity="top|right|end" />
In support library version 24.2.0, the CoordinatorLayout has a new attribute layout_dodgeInsetEdges
that you can add to the LinearLayout (or any other view, for that matter) containing your two FABs, and the Snackbar will move that entire view out of the way!
So, for example:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:fitsSystemWindows="true"
tools:context=".Activities.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom|right"
android:layout_marginRight="@dimen/fab_margin"
android:layout_marginBottom="@dimen/fab_margin"
app:layout_dodgeInsetEdges="bottom"> //THIS IS THE LINE THAT MATTERS
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/fab_margin"
android:src="@drawable/icon"
android:tint="@color/colorTextAndIcons"
app:backgroundTint="@color/colorPrimary"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/fab_margin"
android:src="@drawable/icon"
android:tint="@color/colorTextAndIcons"
app:backgroundTint="@color/colorPrimary"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
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