Floating Action Buttons in Android Lollipop

Bolot Kerimbaev

6 min read

Jul 28, 2014

Floating Action Buttons in Android Lollipop

Note: At the 2015 Google I/O, Android announced their new Design Support Library, which includes the Floating Action Button. You no longer need to create it from scratch. Read my post on Android’s Design Support Library for more information.

At 2014 Google I/O, Google announced a new visual language called Material Design. Material Design guides creation of user experiences that work well on different devices, with different input methods and on different platforms. Google is gradually implementing Material Design in their apps and products, and we can start playing with it, too.

One interesting new design pattern is the promoted action, implemented as a floating action button. This pattern exemplifies the underlying principles of Material Design and is a good place to start adapting existing applications to the new version of Android.

You can see from Google’s guidelines that there are many ways to apply this pattern incorrectly, as evidenced by the various “Don’ts” on that page. Today, I want to show you how to implement a floating action button without creating a “Don’t” yourself.

Float Like a Button, Sting Like an Action

If you are just as excited as I am to get my hands dirty and create clean user interfaces with Google’s Material Design, you have probably already downloaded Android Studio 1.0 and Android Lollipop emulator images. If not, go ahead and do so now.

Our Criminal Intent project is a perfect fit for this tutorial. If you haven’t done so, grab your copy of Android Programming: The Big Nerd Ranch Guide and work through Chapters 7-22. (If you’ve done this a long time ago and can’t locate your code, you can also get the solutions from our website).

In the Criminal Intent app, the “Add New Crime” button appears in the Action Bar. It’s a good candidate to be the promoted action, so we will update the app for Lollipop. This is what our final result will look like:

Floating action button

Replacing the Menu Item

Before we work on the button, we need to remove the “Add New Crime” menu item, by creating the res/menu-v21/fragment_crime_list.xml file:

    <!-- res/menu-v21/fragment_crime_list.xml -->
    <menu xmlns:android="">
        <item android:id="@+id/menu_item_show_subtitle"

Now it’s time to add the button. Since CrimeListFragment is a subclass of ListFragment, we didn’t have to define a layout for it. We’ll do this here, by creating fragment_crime_list.xml in layout-v21:

    <!-- res/layout-v21/fragment_crime_list.xml -->
    <FrameLayout xmlns:android=""
        <include layout="@android:layout/list_content"/>

We will use this layout for Lollipop and fall back to the super implementation for previous versions (in

    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v;
            v = inflater.inflate(R.layout.fragment_crime_list, parent, false);
        } else {
            v = super.onCreateView(inflater, parent, savedInstanceState);

At the time of the original post Lollipop hadn’t come out yet. Because Android L was a preview release, things worked a bit differently. Build.VERSION_CODES.L did exist, but its value was set to 10000. The Android L emulator’s Build.VERSION.SDK_INT was 20. Thus we had to use code KITKAT_WATCH, even though it referred to v20 and not v21. Now that Lollipop is out, we don’t have to play with numbers.

Changing the Look and Feel

So far, the button doesn’t look like the floating action button we want. Let’s change the tint and background:


Better! But it’s still not floating. For that, we need to change its elevation:


Because the button is still rectangular, the elevated button drops a rectangular shadow. Fortunately, we can change the button’s outline and clip it (in

    // - onCreateView()
    View addButton = v.findViewById(;
    addButton.setOutlineProvider(new ViewOutlineProvider() {
        public void getOutline(View view, Outline outline) {
            int diameter = getResources().getDimensionPixelSize(R.dimen.diameter);
            outline.setOval(0, 0, diameter, diameter);

In Lollipop, the API changed a bit. Thanks to our commenters for pointing it out. Instead of setOutline, we now use setOutlineProvider.

The evolution of the button:

Button 1Button 2Button 3Button 4

Adding the Ripple Effect and Shadow

We’re almost done! We just need to do two things. First, we need to bring back the ripple effect. Second, we need to change the size of the shadow when the button is pressed.

To re-enable the ripple, we need to change the button’s background from a static shape to a ripple android:background="@drawable/oval_ripple" and create the oval_ripple drawable:

    <!-- res/drawable/oval_ripple.xml -->
    <ripple xmlns:android=""
            <shape android:shape="oval">
                <solid android:color="?android:colorAccent"/>

The last effect is to increase the size of the shadow, and we need to lift the button up after we push down on it. To make it happen, add one more property to the button: android:stateListAnimator="@anim/button_elevation" and create the button_elevation animation:

    <!-- res/anim/button_elevation.xml -->
    <selector xmlns:android="">
        <item android:state_pressed="true">

These are the values in dimens.xml:

    <!-- res/values/dimens.xml -->
    <dimen name="diameter">48dp</dimen>
    <dimen name="elevation_low">1dp</dimen>
    <dimen name="elevation_high">4dp</dimen>
    <dimen name="add_button_margin">16dp</dimen>

Finally, the button needs to perform the action that used to be accessible via the Action Bar, so we add the OnClickListener:

    addButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

Here’s the zoomed in view of the finished result:

Final floating action button

That’s it! With just a handful of new XML files and code, we kicked off the redesign of the Criminal Intent app and implemented a floating action button. There is a lot more to Material Design; read the guidelines, but keep in mind that they’re still being refined.

Juan Pablo Claude

Reviewer Big Nerd Ranch

During his tenure at BNR, Juan Pablo has taught bootcamps on macOS development, iOS development, Python, and Django. He has also participated in consulting projects in those areas. Juan Pablo is currently a Director of Technology focusing mainly on managing engineers and his interests include Machine Learning and Data Science.

Speak with a Nerd

Schedule a call today! Our team of Nerds are ready to help

Let's Talk

Related Posts

We are ready to discuss your needs.

Not applicable? Click here to schedule a call.

Stay in Touch WITH Big Nerd Ranch News