Search code examples
androidandroid-buttonfloating-action-buttonmaterial-components-androidmaterial-components

How do I customize a Floating Action Button shape?


I am trying to create a custom FAB with a custom shape. I have tried setting the android: background along with the shapeAppearanceOverlay and shapeAppearance property and still no luck.

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/edit_score_fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:shapeAppearance="@drawable/edit_score_fab"
            app:shapeAppearanceOverlay="@drawable/edit_score_fab"
            android:background="@drawable/edit_score_fab"/>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <size
        android:height="5dp"
        android:width="10dp"/>
    <corners android:bottomLeftRadius="4dp" />
    <solid android:color="@color/color_primary"/>
</shape>

My result

enter image description here

What I want

enter image description here


Solution

  • You can use the ExtendedFloatingActionButton with a custom shape appearance.
    Something like:

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

    with:

      <style name="ShapeAppearanceOverlayExtended" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">0dp</item>
        <item name="cornerSizeTopLeft">50%</item>
      </style>
    

    enter image description here