Skip to content

Instantly share code, notes, and snippets.

@mmazzarolo
Last active April 10, 2018 04:47
Show Gist options
  • Save mmazzarolo/a5d780807fdfa93ee7ed to your computer and use it in GitHub Desktop.
Save mmazzarolo/a5d780807fdfa93ee7ed to your computer and use it in GitHub Desktop.
Floating Action Menu like Inbox
<?xml version="1.0" encoding="utf-8"?>
<!--
drawable/fab_label_background.xml
This is the background of the fab labels.
It is similiar to a CardView with rounded corners.
The output of my style is the following:
https://raw.githubusercontent.com/mmazzarolo/easy-bookmarks/master/extras/1.gif
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:dither="true"
android:shape="rectangle">
<corners android:radius="3dp"/>
<solid android:color="@color/#BDBDBD"/>
</shape>
</item>
<item android:bottom="1dp">
<shape
android:dither="true"
android:shape="rectangle">
<corners android:radius="3dp"/>
<solid android:color="@android:color/white"/>
<padding
android:bottom="6dp"
android:left="8dp"
android:right="8dp"
android:top="4dp"/>
<stroke android:color="#BDBDBD"/>
</shape>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<!--
layout/floating_action_menu.xml
Just include this layout inside a CoordinatorLayout:
<include layout="@layout/floating_action_menu"/>
The output of my style is the following:
https://raw.githubusercontent.com/mmazzarolo/easy-bookmarks/master/extras/1.gif
-->
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/floating_action_menu"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|end"
android:padding="16dp"
app:layout_behavior="com.mmazzarolo.dev.easy_bookmarks.FloatingActionMenuBehavior"
fab:menu_backgroundColor="#D2E0E0E0"
fab:menu_colorNormal="#FF5252"
fab:menu_colorPressed="#FF1744"
fab:menu_icon="@drawable/ic_add_white_24dp"
fab:menu_labels_margin="8dp"
fab:menu_labels_style="@style/fab_menu_labels_style"
fab:menu_labels_textSize="14sp">
<com.github.clans.fab.FloatingActionButton
android:id="@+id/floating_action_button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_fab_one"
app:fab_colorNormal="#FF5252"
app:fab_colorPressed="#FF1744"
fab:fab_label="@string/fab_one"
fab:fab_size="mini"/>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/floating_action_button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_fab_two"
app:fab_colorNormal="#FF5252"
app:fab_colorPressed="#FF1744"
fab:fab_label="@string/fab_two"
fab:fab_size="mini"/>
</com.github.clans.fab.FloatingActionMenu>
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.util.AttributeSet;
import android.view.View;
import com.github.clans.fab.FloatingActionMenu;
import java.util.List;
/**
* Created by Matteo on 08/08/2015.
*
* Floating Action Menu Behavior for Clans.FloatingActionButton
* https://github.com/Clans/FloatingActionButton/
*
* Use this behavior as your app:layout_behavior attribute in your Floating Action Menu to use the
* FabMenu in a Coordinator Layout.
*
* Remember to use the correct namespace for the fab:
* xmlns:fab="http://schemas.android.com/apk/res-auto"
*/
public class FloatingActionMenuBehavior extends CoordinatorLayout.Behavior {
private float mTranslationY;
public FloatingActionMenuBehavior(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
if (child instanceof FloatingActionMenu && dependency instanceof Snackbar.SnackbarLayout) {
this.updateTranslation(parent, child, dependency);
}
return false;
}
private void updateTranslation(CoordinatorLayout parent, View child, View dependency) {
float translationY = this.getTranslationY(parent, child);
if (translationY != this.mTranslationY) {
ViewCompat.animate(child)
.cancel();
if (Math.abs(translationY - this.mTranslationY) == (float) dependency.getHeight()) {
ViewCompat.animate(child)
.translationY(translationY)
.setListener((ViewPropertyAnimatorListener) null);
} else {
ViewCompat.setTranslationY(child, translationY);
}
this.mTranslationY = translationY;
}
}
private float getTranslationY(CoordinatorLayout parent, View child) {
float minOffset = 0.0F;
List dependencies = parent.getDependencies(child);
int i = 0;
for (int z = dependencies.size(); i < z; ++i) {
View view = (View) dependencies.get(i);
if (view instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(child, view)) {
minOffset = Math.min(minOffset, ViewCompat.getTranslationY(view) - (float) view.getHeight());
}
}
return minOffset;
}
/**
* onStartNestedScroll and onNestedScroll will hide/show the FabMenu when a scroll is detected.
*/
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child,
View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||
super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,
nestedScrollAxes);
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target,
int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
dyUnconsumed);
FloatingActionMenu fabMenu = (FloatingActionMenu) child;
if (dyConsumed > 0 && !fabMenu.isMenuButtonHidden()) {
fabMenu.hideMenuButton(true);
} else if (dyConsumed < 0 && fabMenu.isMenuButtonHidden()) {
fabMenu.showMenuButton(true);
}
}
}
<!--
values/styles.xml
This is the style for the fab labels.
The output of my style is the following:
https://raw.githubusercontent.com/mmazzarolo/easy-bookmarks/master/extras/1.gif
-->
<style name="fab_menu_labels_style">
<item name="android:background">@drawable/fab_label_background</item>
<item name="android:textColor">#757575</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_marginRight">24dp</item>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment