Search code examples
androidmaterial-designandroid-buttonandroid-stylesfloating-action-button

How to make FloatingActionButton a Square with the Material Library?


I'm trying to make by FloatingActionButton a square using the material library in my styles:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fabSize="mini"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_margin="16dp"/>

Solution

  • You can use ExtendedFloatingActionButton with 0dp corners

    <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:shapeAppearanceOverlay="@style/SquareFAB" />
    

    In styles.xml or themes.xml:

    <style name="SquareFAB" parent="">
        <item name="cornerSize">0dp</item>
    </style>
    

    UPDATE:

    But I want it the same size as my mini fab button i.imgur.com/Qg9kzdu.png which has the attribute app:fabSize="mini"

    As per documentation, the mini size of the FAB is 40dp; so you can adjust that as width & height:

    <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
            android:layout_width="40dp"
            android:layout_height="40dp"
            app:shapeAppearanceOverlay="@style/SquareFAB" />