Part 1 – Floating action button

By -

Welcome to the first part of the tutorial series on Android design support library. As said in the series announcement post, last week I am done with creating a complete example using android design support library and its components. I thought to write this series to share my learning with you and I would take you through the new components, undocumented things and issues lying inside the android design support library. In this part, we will talk about Floating action button.

Floating Action button

Floating action button (FAB) is simply a circle button with rounded shadow, floats above the UI and used to display any promoted action, like adding a new item, compose mail, etc.

Based on the FloatingActionButton class code, the floating button can be an either of two sizes: i) Normal (56dp) or ii) Mini (40dp)

final int getSizeDimension() {
   switch(this.mSize) {
   case 0:
       return this.getResources().getDimensionPixelSize(dimen.fab_size_normal);
   case 1:
       return this.getResources().getDimensionPixelSize(dimen.fab_size_mini);

FloatingActionButton is nothing but a view extending ImageView.

Add Android design support library dependency

First thing first, add a below line to include design support library in gradle file:

compile ''

Step 1:
Include FloatingActionButton in the xml layout file. In addition, assuming xmlns:app=” is declared as namespace the top of your layout.


       android:layout_gravity="bottom|right" />



Developer documentation of Floating action button (FAB) contains description about only fabSize attribute, have reported issue (#178117) already about including details about other attributes, but until then below are some of the attributes:

  • By default Floating action button uses the ascent color given in application theme for its background, but you can use app:backgroundTint attribute or call setBackgroundTintList (ColorStateList tint) method to change the background color.
  • As said above Floating action button size can be either Normal or Mini, you can use app:fabSize attribute (with value either normal or mini).
  • Use android:src attribute to change the drawable for the Floating action button
  • Use app:rippleColor to set the color for the ripple effects when Floating action button pressed
  • Use app:borderWidth to set the border width for the FAQ
  • Use app:elevation to set the idle state’s shadow depth (default is 6dp)
  • Use app:pressedTranslationZ to set the press state’s shadow depth (default is 12dp)

Step 2:
Include the required attributes to display FAB with the image and size.


Step 3:
We can assign the click listener same way we assign to widgets like ImageView and Button!

FloatingActionButton btnFab = (FloatingActionButton) findViewById(;
btnFab.setOnClickListener(new View.OnClickListener() {

   public void onClick(View v) {
       Toast.makeText(MainActivity.this, "Hello FAB!", Toast.LENGTH_SHORT).show();

We are done with the FAB example!


At the time of writing this article, there are some bugs in library for FAB. To see that run this example in Kitkat and Lollipop devices. You would see below output:

FloatingActionButton Lollipop

FloatingActionButton Lollipop

FloatingActionButton Kitkat

FloatingActionButton Kitkat

Issue 1: Margin issue in Android 4.4 and 5.0
It’s clearly noticeable that there is a margin issue in Lollipop. To resolve this issue, define the bottom and the right margin both with 16dp for API 21+ and both with 0dp for pre-lollipop.


<dimen name="fab_margin_right">0dp</dimen>
<dimen name="fab_margin_bottom">0dp</dimen>


<dimen name="fab_margin_right">16dp</dimen>
<dimen name="fab_margin_bottom">16dp</dimen>

and refer the same values in your FAB inside layout:


Issue 2: Shadow issue in Android 5.0
Now, you might have caught shadow issue while checking above screenshots! So the issue is FAB doesn’t have any shadow but has in Kitkat, to resolve this issue in Lollipop, just give border width in FAB:


Issue 3: RotateAnimation doesn’t work on FAB (Issue ID: #176116)

Download source code:
You can download the source code of this example:

In Summary

This bring us to the end of this part. In this part, we have talked about floating action button, attributes and issues. We would look how to animate floating action button based on the certain actions like scrolling RecyclerView up and down, but that I would cover once we are done with exploring CoordinatorLayout.

Read next part about SnackBar, another component of Android Design support library.

Paresh Mayani

Lazy android developer, exploring the horizon of android development since 7 years, currently working as a Technical Lead - Android at Simform Solutions, India. Former Application Architect at KarConnect. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

Loading Facebook Comments ...
Loading Disqus Comments ...