Android FloatingActionBar Speed Dial / Sub Menu

April 22, 2020

I am using leinardi/FloatingActionButtonSpeedDial, or you could try yavski/fab-speed-dial as well.

Edit app / module build.gradle

dependencies {
    implementation "com.leinardi.android:speed-dial:3.1.1"
}

Code

// using menu layout fail to load white icon or use tint
// speedDial.inflate(R.menu.home_speeddial)

speedDial.addAllActionItems(
    listOf(
        SpeedDialActionItem.Builder(R.id.action_new_batch, R.drawable.ic_baseline_local_florist_24)
            .setLabel("Batch")
            .setFabImageTintColor(Color.WHITE)
            .create(),
        SpeedDialActionItem.Builder(R.id.action_new_article, R.drawable.ic_baseline_event_note_24)
            .setLabel("Article")
            .setFabImageTintColor(Color.WHITE)
            .create(),
        SpeedDialActionItem.Builder(R.id.action_new_purchase, R.drawable.ic_baseline_shopping_cart_24)
            .setLabel("Purchase")
            .setFabImageTintColor(Color.WHITE)
            .create()
    )
)

speedDial.setOnActionSelectedListener { actionItem ->
    when (actionItem.id) {
        R.id.action_new_batch -> {
            Toast.makeText(requireContext(), "New Batch", Toast.LENGTH_LONG).show()
            speedDial.close()
            true
        }
        else -> false
    }
}

NOTE: Add icon via Android Studio -> File -> New -> Vector Asset

Layout

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.HomeFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.leinardi.android.speeddial.SpeedDialOverlayLayout
        android:id="@+id/overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.leinardi.android.speeddial.SpeedDialView
        android:id="@+id/speedDial"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:sdMainFabClosedSrc="@drawable/ic_baseline_add_24"
        app:layout_behavior="@string/speeddial_scrolling_view_snackbar_behavior"
        app:sdOverlayLayout="@id/overlay" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

NOTE: Use app:layout_behavior to auto hide during scroll down (must be child of CoordinatorLayout)

Resource ID

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="action_new_batch"/>
    <item type="id" name="action_new_article"/>
    <item type="id" name="action_new_purchase"/>
</resources>
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.