If you already read the first article regarding Navigation View, you may be wondering what else the new Android Design support library can do for you. In this occasion, I´ll talk about Floating Action Button.

Introduction

The Floation Action Button (FAB) is a new component included in Material Design guidelines that emphasizes the most important action in our current screen. It´s a bold and stylish way to attract users attention.

Though there is an enormous hype surrounding this component, it´s really important to create FABs only when they are really necessary, and when they show an action that it´s gonna be used quite often (ideally, all the times the activity is shown). It´s interesting to read other voices that affirm FABs are a bad idea.

Using Floating Action Button

The use of FAB is really simple, though this component has brought some debate these days after the launch of the design library, because some things didn´t work as expected.

I´ll show you the general behaviour, and then some ideas you might take into account to make it work properly.

First, the only thing you need is use a FloatingActionButton , which extends ImageView , so many of the things you know about that control will be helpful. This is the way you add it to you XML:

[xml]

<android.support.design.widget.FloatingActionButton

android:id="@+id/fab"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_add_black"

android:layout_gravity="bottom|end"

app:elevation="6dp"

app:pressedTranslationZ="12dp"/>

[/xml]

Want to learn Kotlin? Check my free guide to create your first project in 15 minutes! GET THE FREE GUIDE

As you see, I only added an icon, an elevation (so that it shows a shadow) and a pressedTranslationZ ; that way the shadow will grow when it´s pressed. A nice effect, and really easy to add this way. I also added some gravity flags to set it in the bottom right part of the screen. This is supposing you use a FrameLayout . You can adapt it to the layout you use.

Extra options

Those previous customizations are the simplest ones. The FAB will take the color from the accent color defined in the theme, and the ripple color from the colorControlHighlight , though all these things are customizable.

For the background color you could do:

[xml]

app:backgroundTint="@color/mycolor"

[/xml]

And for the ripple:

[xml]

app:rippleColor="@android:color/white"

[/xml]

You also have methods available to do all this programmatically, but the backgroundTint is a bit more difficult because it use a state list, so this is the way to go (thanks Carlos Morera for the tip):

[java]

fab.setBackgroundTintList(ColorStateList.valueOf("Your color"));

[/java]

There are two different sizes of FAB available, and it´s easy to get the mini version just by adding another property:

[xml]

app:fabSize="mini"

[/xml]

Cautions

Although these things will be probably fixed in future, here are a couple of tricks to make FAB work as expected. First, if you run previous code, you´ll find a lot of different unexpected behaviours depending on the Android version you are using. All of these problems seem to be caused by a property called borderWidth , which you need to set explicitly to 0:

[xml]

app:borderWidth="0dp"

[/xml]

Besides, you will see that with this XML, pre-lollipop versions will get some margin around, while 21+ won´t get it. The reasoning is the same as what you could see on CardView : Shadows on pre-lollipop are rendered using a drawable, which uses its own space to draw it, while the shadows in Lollipop are rendered by the system. CardView had a property that enabled a compat padding.

But Floating Action Button doesn´t. However, this is really easy to simulate. Just add a padding that is dependent on the android version:

[xml]

android:layout_margin="@dimen/fab_compat_margin"

[/xml]

You now only need to define the value on its proper dimen.xml files.

For values :

[xml]

app:borderWidth="0dp"

[/xml]

For values-v21 :

[xml]

app:borderWidth="16dp"

[/xml]

Easy, right?

Click Event

You can add a click event as usual:

[java]

fab.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View v) {

Snackbar.make(content, "FAB Clicked", Snackbar.LENGTH_SHORT).show();

}

});

[/java]

Conclusion

Though the impression when using Design Support Library is that it is not perfectly polished, it´s true that with some simple tricks it works quite good and saves a lot of time. I´ll keep investigating new components on future articles. Remember you can see all this code and much more in a small working example at Github.

The team from Webucator has created a video from this article. Take a look!

Thanks for reading.

Like this: Like Loading...