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

て烟熏妆下的殇ゞ 提交于 2019-12-03 01:36:05

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:

In your question you said that you tried adjusting android:src, which is obviously the wrong approach because that one is about the content for FloatingActionButton's.

The android:background parameter will change your shape. Do note that it is supposed to be round.

I think this question is just merely not a duplicate because that other question only aks about the color.

You will need to use this in your XML file:

android:background="@drawable/my_shape"
Saurabh Lalwani

What you're looking for is changing the background of your button

android:background="@drawable/my_shape"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!