fab-speed-dial

repository·master·Indexed 19 days ago

https://github.com/yavski/fab-speed-dial

An Android library that combines the Floating Action Button (FAB) pattern with standard Android menu resources to create a Material Design 'Speed Dial' menu. It supports custom XML attributes for styling main and mini FABs, multi-colored mini FAB configurations via integer arrays, and menu event handling through SimpleMenuListenerAdapter.

Tokens
1.7K
Snippets
4
Records
6
Agent score
17%

What's inside fab-speed-dial

  1. How FabSpeedDial handles layout and CoordinatorLayout

    master

    When using FabSpeedDial, be aware of the following layout behaviors:

    • Shadow Padding: For Android SDK versions earlier than 21, the library automatically handles the extra padding required to draw shadows, so you do not need to account for it manually.
    • CoordinatorLayout Margins: In a CoordinatorLayout, standard FAB margin values can sometimes be ignored. To resolve this, FabSpeedDial automatically adds left or right margin values (depending on the gravity) while accounting for the SDK version.
    • Positioning: Always use standard Android APIs to position the view within its parent and ensure app:fabGravity is set to a relevant value (e.g., bottom_end).
  2. Configure multi-colored mini FABs

    master

    To assign different colors to each mini FAB in the list:

    1. Define an <integer-array> resource containing the desired colors.
    2. In your menu.xml, ensure every <item> has an android:orderInCategory attribute assigned in the same 0-based order as your color array.
    3. Use the app:miniFabTitleTextColorList and app:miniFabBackgroundTintList attributes in your FabSpeedDial XML declaration.

    Note: If you use app:miniFabTitleTextColor or app:miniFabBackgroundTint (single values), they will be overridden by the list attributes.

    Example color array:

    <integer-array name="fab_menu_item_colors">
        <item>@android:color/holo_red_dark</item>
        <item>@android:color/holo_purple</item>
        <item>@android:color/holo_green_light</item>
    </integer-array>
    <integer-array name="fab_menu_item_colors">
        <!-- A dark Holo shade of red -->
        <item>@android:color/holo_red_dark</item>
        <!-- A Holo shade of purple -->
        <item>@android:color/holo_purple</item>
        <!-- A light Holo shade of green -->
        <item>@android:color/holo_green_light</item>
    </integer-array>
  3. Implement FabSpeedDial in Layout

    master

    To use FabSpeedDial, follow these three steps:

    1. Define a menu resource in your res/menu/ directory using standard Android <menu> and <item> tags.
    2. Add the FabSpeedDial view to your XML layout.
    3. Assign an ID to the FabSpeedDial view. This is critical: if you do not define an ID, the menu's open/closed state will not be persisted during device rotation.

    Note on positioning: Use standard Android layout attributes to position the view within its parent ViewGroup, and set the app:fabGravity attribute to ensure correct alignment.

    Example XML implementation:

    <io.github.yavski.fabspeeddial.FabSpeedDial
        android:id="@+id/fab_speed_dial"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:fabGravity="bottom_end"
        app:fabMenu="@menu/menu_main"
        app:miniFabBackgroundTint="@android:color/white"
        app:miniFabDrawableTint="?attr/colorPrimaryDark"
        app:miniFabTitleTextColor="?attr/colorPrimaryDark" />
  4. Handle FabSpeedDial menu events

    master

    You can listen for menu preparation and item selection by using setMenuListener with a SimpleMenuListenerAdapter.

    Prepare Menu

    Use onPrepareMenu(NavigationMenu navigationMenu) to modify menu items or return false to prevent the menu from being displayed at all.

    Item Selection

    Use onMenuItemSelected(MenuItem menuItem) to respond when a user selects an item from the speed dial.

    FabSpeedDial fabSpeedDial = (FabSpeedDial) findViewById(R.id.fab_speed_dial);
    fabSpeedDial.setMenuListener(new SimpleMenuListenerAdapter() {
        @Override
        public boolean onPrepareMenu(NavigationMenu navigationMenu) {
            // Modify menu items or return false to hide menu
            return true;
        }
    
        @Override
        public boolean onMenuItemSelected(MenuItem menuItem) {
            // Handle the selection (e.g., start an activity)
            return false;
        }
    });
    FabSpeedDial fabSpeedDial = (FabSpeedDial) findViewById(R.id.fab_speed_dial);
    fabSpeedDial.setMenuListener(new SimpleMenuListenerAdapter() {
        @Override
        public boolean onPrepareMenu(NavigationMenu navigationMenu) {
            // TODO: Do something with yout menu items, or return false if you don't want to show them
            return true;
        }
    });
  5. Reference: FabSpeedDial XML attributes

    master

    The following attributes are available for customizing the FabSpeedDial component. Attributes prefixed with fab apply to the main FAB, while those prefixed with miniFab apply to the items in the expanded menu.

    AttributeAndroid EquivalentDescription
    app:fabDrawableandroid:srcSets the icon drawable of the main FAB
    app:fabDrawableTintandroid:tintTints the icon drawable of the main FAB
    app:fabBackgroundTintandroid:backgroundTintTints the background colour of the main FAB
    app:miniFabDrawableTintandroid:tintTints the icon drawable of the mini FAB(s)
    app:miniFabBackgroundTintandroid:backgroundTintTints the background colour of the mini FAB(s)
    app:miniFabBackgroundTintListAn array containing the background colors for each of the mini FABs
    app:miniFabTitleTextColorandroid:textColorSets the color of the title of the mini FAB
    app:miniFabTitleTextColorListAn array containing the colors for each of the titles of the mini FABs
    app:miniFabTitleBackgroundTintTints the background colour of the title(s) of the mini FAB(s)
    app:miniFabTitlesEnabledConvenience for hiding the title(s) of the mini FAB(s)
    app:touchGuardHide FAB when touching out of its bounds
    app:touchGuardDrawableandroid:backgroundSets background to the container of FAB