130

Recently, I read these posts:

But, none of them give me a detail example about creating a new FloatingActionButton. It's so hard to understand, which is why I am asking this question.

Can anyone give me an example about it?

EDIT

I just found some issues on FloatingActionButton (FAB), and I want to improve another answer. See my answer below.

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
  • http://stackoverflow.com/questions/30483210/is-there-a-native-component-for-the-floating-action-button-in-android-material-d/30527761#30527761 – Gabriele Mariotti Jun 02 '15 at 06:47

5 Answers5

273

So in your build.gradle file, add this:

compile 'com.android.support:design:27.1.1'

AndroidX Note: Google is introducing new AndroidX extension libraries to replace the older Support Libraries. To use AndroidX, first make sure you've updated your gradle.properties file, edited build.gradle to set compileSdkVersion to 28 (or higher), and use the following line instead of the previous compile one.

implementation 'com.google.android.material:material:1.0.0'

Next, in your themes.xml or styles.xml or whatever, make sure you set this- it's your app's accent color-- and the color of your FAB unless you override it (see below):

        <item name="colorAccent">@color/floating_action_button_color</item>

In the layout's XML:

<RelativeLayout
 ...
 xmlns:app="http://schemas.android.com/apk/res-auto">

       <android.support.design.widget.FloatingActionButton
            android:id="@+id/myFAB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_plus_sign"
            app:elevation="4dp"
            ... />

</RelativeLayout>

Or if you are using the AndroidX material library above, you'd instead use this:

<RelativeLayout
 ...
 xmlns:app="http://schemas.android.com/apk/res-auto">

       <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/myFAB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:srcCompat="@drawable/ic_plus_sign"
            app:elevation="4dp"
            ... />

</RelativeLayout>

You can see more options in the docs (material docs here) (setRippleColor, etc.), but one of note is:

    app:fabSize="mini"

Another interesting one-- to change the background color of just one FAB, add:

    app:backgroundTint="#FF0000"

(for example to change it to red) to the XML above.

Anyway, in code, after the Activity/Fragment's view is inflated....

    FloatingActionButton myFab = (FloatingActionButton)  myView.findViewById(R.id.myFAB);
    myFab.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doMyThing();
        }
    });

Observations:

  • If you have one of those buttons that's on a "seam" splitting two views (using a RelativeLayout, for example) with, say, a negative bottom layout margin to overlap the border, you'll notice an issue: the FAB's size is actually very different on lollipop vs. pre-lollipop. You can actually see this in AS's visual layout editor when you flip between APIs-- it suddenly "puffs out" when you switch to pre-lollipop. The reason for the extra size seems to be that the shadow expands the size of the view in every direction. So you have to account for this when you're adjusting the FAB's margins if it's close to other stuff.
  • Here's a way to remove or change the padding if there's too much:

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) myFab.getLayoutParams();
        p.setMargins(0, 0, 0, 0); // get rid of margins since shadow area is now the margin
        myFab.setLayoutParams(p);
    }
    
  • Also, I was going to programmatically place the FAB on the "seam" between two areas in a RelativeLayout by grabbing the FAB's height, dividing by two, and using that as the margin offset. But myFab.getHeight() returned zero, even after the view was inflated, it seemed. Instead I used a ViewTreeObserver to get the height only after it's laid out and then set the position. See this tip here. It looked like this:

        ViewTreeObserver viewTreeObserver = closeButton.getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                        closeButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } else {
                        closeButton.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                    // not sure the above is equivalent, but that's beside the point for this example...
                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) closeButton.getLayoutParams();
                    params.setMargins(0, 0, 16, -closeButton.getHeight() / 2); // (int left, int top, int right, int bottom)
                    closeButton.setLayoutParams(params);
                }
            });
        }
    

    Not sure if this is the right way to do it, but it seems to work.

  • It seems you can make the shadow-space of the button smaller by decreasing the elevation.
  • If you want the FAB on a "seam" you can use layout_anchor and layout_anchorGravity here is an example:

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"
        android:src="@drawable/ic_discuss"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"/>
    

Remember that you can automatically have the button jump out of the way when a Snackbar comes up by wrapping it in a CoordinatorLayout.

More:

fattire
  • 6,823
  • 3
  • 25
  • 38
  • Setting "colorAccent" color sets the same color to other controls. Is there a way to set different color for FAB? – joshas Jun 02 '15 at 20:22
  • 1
    Sure. Just add `android:backgroundTint="#FF0000"` for example to to the FloatingActionBar in the XML. (adding to answer) – fattire Jun 02 '15 at 20:55
  • Cool. Can someone else confirm/advise? I tested w/"android:" and it worked.. but maybe app: is a better way to do it...? – fattire Jun 03 '15 at 06:06
  • @fattire Just wondering, can we add a Quick Return effect for FAB using design support library? – Dark Leonhart Jun 03 '15 at 15:44
  • actually I am getting a crash when using android:backgroundTint in the xml for overriding the accent color, but app:backgroundTint seems to work fine (tested on Lollipop) – shalafi Jun 04 '15 at 15:15
  • 1
    Hmm. Okay I'll change it above then. Thanks! Dark Leonhart-- I haven't seen anything about quick return built-in but there are samples of doing it elsewhere on stackoverflow that I presume will still work. Plus some libraries are available that do it as well. – fattire Jun 04 '15 at 22:02
  • 1
    This component, as of 22.2.0, in my opinion, is not production ready. Btw, may be a typo, but don't use `sp` in the evelation property. It should be `app:elevation="4dp"` – Joao Sousa Jun 06 '15 at 11:06
  • Joao yeah it has some issues-- changed sp to dp in example above, thx for the catch... – fattire Jun 07 '15 at 04:20
  • 1
    Note if you are adding a `FloatingActionButton` to a `CoordinatorLayout`, you can use `app:layout_anchor="element_with_seam"` and `app:layout_anchorGravity="bottom|right|end"` to position the FAB correctly over a seam - see the [activity_detail layout from cheesesquare](https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/layout/activity_detail.xml) – ianhanniballake Jun 07 '15 at 04:54
  • 2
    @DarkLeonhart - check out my [example of showing/hiding the FAB on scroll](https://plus.google.com/+IanLake/posts/haQL1mnTzaw) by extending its behavior. – ianhanniballake Jun 07 '15 at 04:55
  • What if you're using ecplise? I've added the support library to the project but keep getting errors. It says it can't find any 'app:...' properties and if I remove those it wont build still. – Knoxie Jun 09 '15 at 14:04
  • @Knoxie Not using eclipse any more, but be sure you have the `xmlns:app="http://schemas.android.com/apk/res-auto"` part in a [ViewGroup](https://developer.android.com/reference/android/view/ViewGroup.html") that contains the FAB. – fattire Jun 10 '15 at 06:44
  • how about background color in xml ? – MBH Nov 07 '15 at 18:56
  • What about the color of pressing the FAB ? – android developer Mar 01 '16 at 16:30
  • What's the equivalent of ' – IainCunningham Sep 25 '18 at 10:44
  • The button appears at the top left of the screen, but isn't it supposed to be at the bottom right? – Richard Barraclough Jun 10 '20 at 11:25
49

I just found some issues on FAB and I want to enhance another answer.


setRippleColor issue

So, the issue will come once you set the ripple color (FAB color on pressed) programmatically through setRippleColor. But, we still have an alternative way to set it, i.e. by calling:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
ColorStateList rippleColor = ContextCompat.getColorStateList(context, R.color.fab_ripple_color);
fab.setBackgroundTintList(rippleColor);

Your project need to has this structure:

/res/color/fab_ripple_color.xml

enter image description here

And the code from fab_ripple_color.xml is:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/fab_color_pressed" />
    <item android:state_focused="true" android:color="@color/fab_color_pressed" />
    <item android:color="@color/fab_color_normal"/>
</selector>

Finally, alter your FAB slightly:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_action_add"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    app:fabSize="normal"
    app:borderWidth="0dp"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp"
    app:rippleColor="@android:color/transparent"/> <!-- set to transparent color -->

For API level 21 and higher, set margin right and bottom to 24dp:

...
android:layout_marginRight="24dp"
android:layout_marginBottom="24dp" />

FloatingActionButton design guides

As you can see on my FAB xml code above, I set:

        ...
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        app:elevation="6dp"
        app:pressedTranslationZ="12dp"
        ...
  • By setting these attributes, you don't need to set layout_marginTop and layout_marginRight again (only on pre-Lollipop). Android will place it automatically on the right corned side of the screen, which the same as normal FAB in Android Lollipop.

        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
    

Or, you can use this in CoordinatorLayout:

        android:layout_gravity="end|bottom"
  • You need to have 6dp elevation and 12dp pressedTranslationZ, according to this guide from Google.

FAB rules

Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
  • 5
    thumbs up for the research involved. plus 1 for the `pressedTranslationZ`. I didn't know that. – Joao Sousa Jun 06 '15 at 11:08
  • app:elevation="6dp" app:pressedTranslationZ="12dp" does the trick for pre-lollipop, but my fab is literally on the corner of screen on Lollipop – Thiago Jun 19 '15 at 19:36
  • 1
    You are using an incorrect value for "pressedTranslationZ". It's not the value of elevation on pressed state. PressedElevation = elevation + pressedTranslationZ. So correct values is app:elevation="6dp" app:pressedTranslationZ="6dp" – e.shishkin Jul 23 '15 at 10:11
  • 3
    Actually **you don't need** to set the `app:elevation` and `app:pressedTranslationZ` values. Their default value is the one defined in the Material design guidelines. Anyway, e.shishkin is right, the values are `app:elevation:6dp` and `pressedTranslationZ:6dp`. You can check it in the com.android.support.design [source code](https://github.com/android/platform_frameworks_support/search?utf8=%E2%9C%93&q=design_fab_translation_z_pressed&type=Code) – Christian García Nov 27 '15 at 15:37
12

FloatingActionButton extends ImageView. So, it's simple as like introducing an ImageView in your layout. Here is an XML sample.

<android.support.design.widget.FloatingActionButton   xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/somedrawable"
    android:layout_gravity="right|bottom"
    app:borderWidth="0dp"
    app:rippleColor="#ffffff"/>

app:borderWidth="0dp" is added as a workaround for elevation issues.

siriscac
  • 1,699
  • 12
  • 21
4

for AndroidX use like

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/ic_add" />
Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40
1

If you already added all libraries and it still doesn't work use:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/ic_add" 
/>

instead of:

<android.support.design.widget.FloatingActionButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   app:srcCompat="@drawable/ic_add"
 />

And all will work fine :)

Dejv
  • 944
  • 2
  • 14
  • 31
Max Shwed
  • 232
  • 2
  • 10