FloatingMenu Android Library

repository·master·Indexed 43 days ago

https://github.com/rjsvieira/floatingmenu

An Android library for creating customizable floating action buttons with sub-button layouts and animation styles such as radial or expand. Supports XML configuration via FloatingMenuButton and programmatic addition of FloatingSubButton elements. Version 1.3.0 requires a minimum SDK of 15.

Tokens
2.4K
Snippets
6
Records
6
Agent score
5%

What's inside FloatingMenu

  1. Install FloatingMenu via JitPack

    master

    To include FloatingMenu in your Android project, follow these two steps:

    1. Add the JitPack repository to your root build.gradle file.
    2. Add the dependency to your app-level build.gradle file.

    Minimum SDK requirement: 15

    // In your root/build.gradle
    allprojects {
      repositories {
        ...
        maven { url 'https://jitpack.io' }
      }
    }
    
    // In your app/build.gradle
    dependencies {
      compile 'com.github.rjsvieira:floatingMenu:1.3.0'
    }
  2. Configure FloatingMenuButton via XML

    master

    You can define a FloatingMenuButton in your layout XML. The button acts as a container that only accepts FloatingSubButton children.

    Key attributes for FloatingMenuButton include:

    • floatingMenuActionButton:animationType: Sets the open/close animation (e.g., radial or expand).
    • floatingMenuActionButton:dispositionStartAngle: The starting angle for button placement.
    • floatingMenuActionButton:dispositionEndAngle: The ending angle for button placement.
    • floatingMenuActionButton:subActionButtonRadius: The distance between the central button and its children.
    <rjsv.floatingmenu.floatingmenubutton.FloatingMenuButton
      android:id="@+id/my_floating_button"
      android:layout_width="40dp"
      android:layout_height="40dp"
      android:scaleType="centerInside"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      floatingMenuActionButton:animationType="radial"
      floatingMenuActionButton:dispositionEndAngle="360"
      floatingMenuActionButton:dispositionStartAngle="0"
      floatingMenuActionButton:subActionButtonRadius="100">
    
      <rjsv.floatingmenu.floatingmenubutton.subbutton.FloatingSubButton
        android:id="@+id/sub_button_1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/one" />
    
      <!-- Add more FloatingSubButton elements here -->
    
    </rjsv.floatingmenu.floatingmenubutton.FloatingMenuButton>
  3. Implement FloatingMenuButtonStateChangeListener

    master

    To react to the menu opening or closing, implement the FloatingMenuButtonStateChangeListener interface and register it with your FloatingMenuButton instance.

    public interface FloatingMenuButtonStateChangeListener {
        void onMenuOpened(FloatingMenuButton menu);
        void onMenuClosed(FloatingMenuButton menu);
    }
  4. Configure FloatingMenuButton programmatically

    master

    You can configure the FloatingMenuButton and its animation behavior in Java code using a fluent API. Use set... methods on the button for layout/logic properties and getAnimationHandler().set... methods for animation-specific properties like duration, interpolators, and effects (fade, scale, rotate).

    floatingButton = (FloatingMenuButton) findViewById(R.id.my_floating_button);
    
    // Configure layout and movement
    floatingButton.setStartAngle(0)
            .setEndAngle(360)
            .setAnimationType(AnimationType.EXPAND)
            .setMovementStyle(MovementStyle.STICKED_TO_SIDES);
    
    // Configure animation details via the AnimationHandler
    floatingButton.getAnimationHandler()
            .setOpeningAnimationDuration(500)
            .setClosingAnimationDuration(200)
            .setLagBetweenItems(0)
            .setOpeningInterpolator(new FastOutSlowInInterpolator())
            .setClosingInterpolator(new FastOutLinearInInterpolator())
            .shouldFade(true)
            .shouldScale(true)
            .shouldRotate(false);
  5. Add FloatingSubButton programmatically

    master

    If you need to add sub-buttons at runtime, instantiate a FloatingSubButton and use the addFloatingSubButton method on your FloatingMenuButton instance. You can provide optional ViewGroup.LayoutParams to control the sub-button's layout.

    // Create the button and set its background
    FloatingSubButton floatingSubButton = new FloatingSubButton(this);
    floatingSubButton.setBackground(getDrawable(R.drawable.four));
    
    // Add to the parent FloatingMenuButton with default parameters
    floatingButton.addFloatingSubButton(floatingSubButton);
    
    // OR add with custom layout parameters
    floatingButton.addFloatingSubButton(floatingSubButton, customLayoutParameters);
  6. Reference: FloatingMenuButton XML Attributes

    master

    The following attributes are available for configuring the FloatingMenuButton via XML:

    | Attribute        | Type           | Default | Description |
    | ------------- |:-------------:| :-----| :--- |
    | startAngle           | int | 0 | The starting angle for button disposition |
    | endAngle             | int | 180 | The ending angle for button disposition |
    | radius               | int | 100(dp) | The distance between the central button and its children |
    | movementStyle        | MovementStyle (Enumerator) | MovementStyle.FREE | Configures whether the user can or not drag the FloatingMenu around |
    | animationType        | AnimationType (Enumerator) | AnimationType.EXPAND | The open/close animation for FloatingMenuButton |
    | openingDuration      | int | 500 | The opening duration, in milliseconds, of the animation |
    | closingDuration      | int | 500 | The closing duration, in milliseconds, of the animation |
    | lagBetweenItems      | int | 100 | The lag duration between animating items. Affects only AnimationType.EXPAND |
    | openingInterpolator  | Interpolator | OvershootInterpolator | The opening interpolator |
    | closingInterpolator  | Interpolator | AccelerateDecelerateInterpolator | The closing interpolator |
    | shouldRotate         | boolean | true | Specify whether the items should rotate while animating. Available only in AnimationType.EXPAND |
    | shouldFade           | boolean | true | Specify whether the items should fade while animating. Available only in AnimationType.EXPAND |
    | shouldScale          | boolean | true | Specify whether the items should scale while animating. Available only in AnimationType.EXPAND |