-
Notifications
You must be signed in to change notification settings - Fork 39
FloatingActionMenu
This wiki hightlights the setup needed for using the FloatingActionMenu
and the customization options available. It is designed as a step by step guide with simple instructions. Here are a few reasons for switching to this implementation:
- Uses the AppCompat Design Library
FloatingActionButton
and thus has native elevation on Lollipop and above devices and even a shadow animation on pre-Lollipop devices! - Allows easy configuration of you main
FloatingActionButton
's icon animation: apply any rotation and optionally an alpha transition to a second icon (see Google's Inbox). - Works on all devices back to API level 7 just like the AppCompat Design Library!
Ready? Then let's dive in deeper.
Unlike other implementations out there this one doesn't automatically create the main button for you, instead it will assume that the first button in your layout is the main button. Don't panic though: this is a bonus for you! You can do with your main button whatever you want to. And the library still provides a basic setup for the main button.
Also, credits go to Jerzy Chalupski whose FloatingActionMenu I used as a base for developement. I have changed a lot though to support additional animations and the FloatingActionButton
from the AppCompat Design Library.
The setup of the layout is actually quite straightforward. A sample FloatingActionMenu
might look something like this (this one is setup with an inbox-like icon animation and labels):
<com.tr4android.support.extension.widget.FloatingActionMenu
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
app:fabMenuCloseIconAngle="135"
app:fabMenuCloseIconSrc="@drawable/ic_edit_white_24dp"
app:fabMenuLabelStyle="@style/label_style_dark">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_add_white_24dp" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/fam_document"
android:src="@drawable/ic_insert_drive_file_white_24dp"
app:backgroundTint="@color/material_blue"
app:fabSize="mini" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/fam_spreadsheet"
android:src="@drawable/ic_grid_on_white_24dp"
app:backgroundTint="@color/material_green"
app:fabSize="mini" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/fam_presentation"
android:src="@drawable/ic_insert_chart_white_24dp"
app:backgroundTint="@color/material_yellow"
app:fabSize="mini" />
</com.tr4android.support.extension.widget.FloatingActionMenu>
As you can see there are quite a few customization options available that allow easy adaption of the FloatingActionMenu
to your specific situation. Here's a complete list:
-
app:fabMenuExpandDirection
: The direction in which theFloatingActionMenu
should expand. Must be one of eitherup
,down
,left
orright
. Defaults toup
. -
app:fabMenuLabelPosition
: The position of the labels, if enabled. Labels are currently only available when theapp:fabMenuExpandDirection
isup
ordown
. Must be one of eitherleft
orright
. Defaults toleft
. -
app:fabMenuLabelStyle
: The style to apply to the labels. Setting a style also automatically enables the labels. There are two default styles shipped with this library (label_style_dark
andlabel_style_light
), but you can also use any other custom style. Defaults to 0 (no style set). -
app:fabMenuCloseIconAngle
: The angle of rotation to apply to the main button's icon when theFloatingActionMenu
is in expanded state. For simplicity the collapsed state rotation is considered to be 0. Defaults to 0 (no rotation). -
app:fabMenuCloseIconSrc
: Optional drawable for the main button's icon when theFloatingActionMenu
is in expanded state. This applies an alpha transition in addition to the rotation to create a customizable inbox-like effect. Defaults tonull
(no additional drawable).
If you have labels enabled by setting the app:fabMenuLabelStyle
attribute you'll also have to make sure to set the android:contentDescription
attribute of all your child FloatingActionButton
s (excluding the main button) to their proper values as they will be used for the label text. That's it!
If you want the FloatingActionMenu
to handle background dimming for you, you'll have to include a View
in your layout for this:
<...Layout>
<!-- your content -->
<View
android:id="@+id/dimming_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- the floating action menu -->
</...Layout>
Make sure that this view gets declared after the main content, but before the FloatingActionMenu
. Check the activity_sample.xml if you're unsure where to place it.
After that the only thing left to do, is to setup your FloatingActionMenu
with this View
in the onCreate()
method of your activity:
FloatingActionMenu floatingActionMenu = (FloatingActionMenu) findViewById(R.id.fab_menu);
floatingActionMenu.setupWithDimmingView(findViewById(R.id.dimming_view), Color.parseColor("#99000000"));
Congrats! You've made it through the setup. If you have any issues, bugs or feature requests please open a new issue.