Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the shape of Floating Action Button (FAB) on android?

Tags:

In our android app we need to create a floating action button which isn't the default circle but a square with with three rounded corners. The fab looks like in the image below:

enter image description here

I managed to create such a form but don't know how to apply it to my fab, or if it's even possible. The xml file for the shape looks like this:

   <shape xmlns:android="http://schemas.android.com/apk/res/android"       android:shape="rectangle">     <solid android:color="@color/red" />     <corners       android:topLeftRadius="90dp"       android:topRightRadius="90dp"       android:bottomRightRadius="90dp"/>    </shape> 

I tried to change the form with

android:src="@drawable/my_shape" 

but this changes only the icon inside my button. I think I need to extend FloatingActionButton but I have no idea how.

Is there a way to change the shape of the fab? If someone has already achieved this I would appreciate to see a sample.

like image 972
AthaMit Avatar asked Mar 13 '18 16:03

AthaMit


People also ask

How do I change the shape of the floating action button on Android?

To change the shape of the Floating action button: You can use the shape property of FloatingActionButton() widget class. Implement BeveledRectangleBorder( ) on this property. The output will be a square floating action button, increase the border-radius value to make a circular type shape.

How do I change the floating action button image?

Add the floating action button to your layout The size of the FAB, using the app:fabSize attribute or the setSize() method. The ripple color of the FAB, using the app:rippleColor attribute or the setRippleColor() method. The FAB icon, using the android:src attribute or the setImageDrawable() method.


2 Answers

The FloatingActionButton provided by the support libraries can not be extended as the methods that would need to be replaced are set to private. Material Components (which is the official AndroidX replacement for the support library) has Shape Theming on the roadmap for ~October-December, which will let you do just that officially and easily (without having to extend the view).

But it's not available just yet, so in the meantime, I forked customFloatingActionButton by robertlevonyan, and with some slight modifications, it allows you to use your own custom shape. The source code is available here.

To add it to your project with Gradle, you'll need to use jitpack. You can add it like this:

allprojects {    repositories {        ...        maven { url 'https://jitpack.io' }    } } 

Then implement my fork:

implementation 'com.github.JediBurrell:customFloatingActionButton:-SNAPSHOT' 

Next we'll create the shape, the one you created should work fine.

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">     <corners         android:topLeftRadius="@dimen/fab_circle_radius"         android:topRightRadius="@dimen/fab_circle_radius"         android:bottomRightRadius="@dimen/fab_circle_radius" />     <solid android:color="@color/colorAccent" /> </shape> 

Then finally add it to your layout (more FAB options are listed in the Github repo):

<com.jediburrell.customfab.FloatingActionButton     android:id="@+id/floating_action_button"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="bottom|right"     android:layout_margin="16dp"     app:fabType="custom"     app:fabShape="@drawable/fab_teardrop"     app:fabIcon="@drawable/ic_add_24dp"     app:fabColor="@color/red" /> 

And here's how that looks:

like image 168
JediBurrell Avatar answered Sep 17 '22 21:09

JediBurrell


With the Material Components Library you can use the shapeAppearanceOverlay:

<com.google.android.material.floatingactionbutton.FloatingActionButton     app:shapeAppearanceOverlay="@style/fab_3_rounded"     .../> 

with:

<style name="fab_3_rounded">     <item name="cornerFamily">rounded</item>     <item name="cornerSize">50%</item>     <item name="cornerSizeBottomLeft">0dp</item> </style> 

enter image description here

like image 37
Gabriele Mariotti Avatar answered Sep 21 '22 21:09

Gabriele Mariotti