Floating Action Button Speed Dial

repository·release·Indexed 23 days ago

https://github.com/leinardi/floatingactionbuttonspeeddial

A library providing Material Design Floating Action Button Speed Dial implementations for both Android Jetpack Compose and the classic Android View system. It includes support for action items with labels, touch guard overlays, and scrolling behavior for CoordinatorLayout. Available as separate dependencies for Compose (com.leinardi.android:speed-dial.compose) and classic View (com.leinardi.android:speed-dial).

Tokens
2.7K
Snippets
6
Records
10
Agent score
31%

What's inside floatingactionbuttonspeeddial

  1. Add a Touch Guard Overlay (Classic View)

    release

    To prevent user interaction with the background when the Speed Dial is open (similar to Gmail Inbox), add a SpeedDialOverlayLayout to your layout and link it to the SpeedDialView.

    Implementation

    1. Add SpeedDialOverlayLayout to your XML.
    2. Reference it in SpeedDialView using app:sdOverlayLayout or via setSpeedDialOverlayLayout() in code.
    <!-- XML approach -->
    <com.leinardi.android.speeddial.SpeedDialOverlayLayout 
        android:id="@+id/overlay" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" />
    
    <com.leinardi.android.speeddial.SpeedDialView 
        ... 
        app:sdOverlayLayout="@id/overlay" />
    // Programmatic approach
    val overlayLayout = findViewById<SpeedDialOverlayLayout>(R.id.overlay)
    speedDialView.setSpeedDialOverlayLayout(overlayLayout)
  2. Configure Scrolling Behavior for SpeedDialView

    release

    To hide the FAB when a RecyclerView or NestedScrollView is scrolled, apply the ScrollingViewSnackbarBehavior.

    Note: For this to work, SpeedDialView must be a direct child of a CoordinatorLayout.

    Implementation

    • XML: Use app:layout_behavior="@string/speeddial_scrolling_view_snackbar_behavior".
    • Programmatic: Set the behavior on the CoordinatorLayout.LayoutParams.
    <!-- XML approach -->
    <com.leinardi.android.speeddial.SpeedDialView 
        ... 
        app:layout_behavior="@string/speeddial_scrolling_view_snackbar_behavior" />
    // Programmatic approach
    val params = speedDialView.layoutParams as CoordinatorLayout.LayoutParams
    params.behavior = SpeedDialView.ScrollingViewSnackbarBehavior()
    speedDialView.requestLayout()
  3. Use Speed Dial in Jetpack Compose

    release

    To use the Speed Dial in Compose, add the SpeedDial() Composable to the floatingActionButton slot of a Scaffold. You must manage the SpeedDialState and optionally use SpeedDialOverlay to create a touch guard when the menu is expanded.

    Implementation Steps:

    1. Create a SpeedDialState using rememberSaveable.
    2. Use SpeedDial() and handle onFabClick to toggle the state.
    3. Add action items using the item { ... } block containing FabWithLabel components.
    var speedDialState by rememberSaveable { mutableStateOf(SpeedDialState.Collapsed) }
    var overlayVisible: Boolean by rememberSaveable { mutableStateOf(speedDialState.isExpanded()) }
    Scaffold(
        floatingActionButton = {
            SpeedDial(
                state = speedDialState,
                onFabClick = {
                    overlayVisible = !it
                    speedDialState = SpeedDialState(!it)
                },
            ) {
                item {
                    FabWithLabel(
                        onClick = { showToast(context, "Item 1 clicked!") },
                        labelContent = { Text(text = "Item 1") },
                    ) {
                        Icon(Icons.Default.Share, null)
                    }
                }
                item {
                    FabWithLabel(
                        onClick = { showToast(context, "Item 2 clicked!") },
                        labelContent = { Text(text = "Item 2") },
                    ) {
                        Icon(Icons.Default.Share, null)
                    }
                }
            }
        }
    ) {
        SpeedDialOverlay(
            visible = overlayVisible,
            onClick = {
                overlayVisible = false
                speedDialState = speedDialState.toggle()
            },
        )
    }
  4. Use Speed Dial in Classic Android View

    release

    For XML-based layouts, use the SpeedDialView component. You can add action items programmatically using SpeedDialActionItem.Builder or by inflating a Menu resource.

    XML Setup

    Add the SpeedDialView to your layout file. You can specify the closed state icon using app:sdMainFabClosedSrc.

    Adding Items

    • Programmatically: Use addActionItem with a SpeedDialActionItem.Builder.
    • Menu Resource: Use inflate(R.menu.menu_name) to load items from a standard Android menu resource. Note that only android:id, android:icon, and android:title are supported via inflation.
    <!-- XML Layout -->
    <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_add_white_24dp" />
    // Programmatic Item Addition
    val speedDialView = findViewById<SpeedDialView>(R.id.speedDial)
    speedDialView.addActionItem(
        SpeedDialActionItem.Builder(R.id.fab_no_label, R.drawable.ic_link_white_24dp)
            .create()
    )
    
    // Menu Inflation
    speedDialView.inflate(R.menu.menu_speed_dial)
  5. Install Floating Action Button Speed Dial

    release

    The library provides separate dependencies for Jetpack Compose and the classic Android View system. Official releases are available on Maven Central.

    Compose only

    Use this dependency if you are building a Compose-only UI.

    Classic View only

    Use this dependency for traditional XML-based layouts.

    // Compose only
    implementation "com.leinardi.android:speed-dial.compose:1.0.0-alpha04"
    
    // Classic View only
    implementation "com.leinardi.android:speed-dial:3.3.0"
  6. Handle Speed Dial Events in Classic View

    release

    You can listen for action selections or main FAB changes using the following listeners:

    OnActionSelectedListener

    Triggered when one of the speed dial action items is clicked. Returning true from the listener will close the Speed Dial with animation; returning false will close it without animation.

    OnChangeListener

    Allows you to react to the main FAB click (onMainActionSelected) or changes in the open/closed state (onToggleChanged).

  7. Customize SpeedDialActionItem in Classic View

    release

    Use SpeedDialActionItem.Builder to customize the appearance of individual action items. Available customization options include:

    • setFabBackgroundColor(color: Int)
    • setFabImageTintColor(color: Int)
    • setLabel(text: String)
    • setLabelColor(color: Int)
    • setLabelBackgroundColor(color: Int)
    • setLabelClickable(boolean)
    • setTheme(styleRes: Int): Applies a specific theme to change FAB background and ripple colors.
    speedDialView.addActionItem(
        SpeedDialActionItem.Builder(R.id.fab_custom_color, drawable)
            .setFabBackgroundColor(ResourcesCompat.getColor(resources, R.color.material_white_1000, getTheme()))
            .setFabImageTintColor(ResourcesCompat.getColor(resources, R.color.inbox_primary, getTheme()))
            .setLabel(getString(R.string.label_custom_color))
            .setLabelColor(Color.WHITE)
            .setLabelBackgroundColor(ResourcesCompat.getColor(resources, R.color.inbox_primary, getTheme()))
            .setLabelClickable(false)
            .create()
    )
  8. Add items to SpeedDial using SpeedDialScope.item()

    release

    When using the SpeedDial component in Jetpack Compose, you can define individual speed dial items within the SpeedDialScope using the item function. Each item call takes a composable lambda with SpeedDialItemScope as its receiver, allowing you to configure the content of that specific speed dial entry.

    Note that items added via item are inserted at the beginning of the list (index 0) within the scope's internal interval list.

  9. Use SpeedDialItemScope for configuring speed dial items in Compose

    release
    When using the SpeedDial component in Jetpack Compose, the itemContent lambda provides a SpeedDialItemScope. This scope is used to configure the individual items within the speed dial. It is marked as @Stable to ensure efficient recomposition within the Compose runtime.