A simple Floating Context Menu

A simple Floating Context Menu

FloatingContextMenu

A simple Floating Context Menu.

FloatingContextMenu

Installation

gradle

dependencies {
    implementation 'academy.appdev:floating-context-menu:1.0.2'
}

maven

<dependency>
	<groupId>academy.appdev</groupId>
	<artifactId>floating-context-menu</artifactId>
	<version>1.0.2</version>
	<type>pom</type>
</dependency>

Setup

1) Add FloatingMenu view to you layout and configure it

Anko

menu = floatingMenu(ArrayList<FloatingMenu.MenuItem>().apply {
                    (0..2).forEach { add(FloatingMenu.MenuItem(R.drawable.search_icon, "Option$it")) }
                }) {
                    // configuration
                    menuItemSize = 70
                    menuCorner = 130
                    radius = 130
                    animationDuration = 250L

                }.lparams {
                    width = matchParent
                    height = matchParent
                }

XML

    <appdev.academy.floatingcontextmenu.FloatingMenu
            android:id="@+id/floating_menu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

Configure view in your presenter class :

 floating_menu.apply {
             // configuration
             menuItemSize = 70
             menuCorner = 130
             radius = 130
             animationDuration = 250L
         }

2) Let FloatingMenu handle touch events

Override dispatchTouchEvent method of your activity:

override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        // Let Floating menu handle event if it can
        return if (FloatingMenu.interceptTouch(ev))
            true
        else
            super.dispatchTouchEvent(ev)
            }

3) Show menu

Detect long tap event for example:

setOnItemLongClickListener { _, _, position, _ ->
                        menu.apply {
                            onItemSelected = { toast("Item $position menu option $it") }
                            onLongTapDetected()
                        }
                        true
                    }

Configuration

Floating menu has many public fields that can be modified

    // customization
    var menuItemSize = 70
    var menuCorner = 130
    var radius = 130
    var animationDuration = 250L

    var collapseScale = 0.3f
    var defaultScale = 1f
    var selectedScale = 1.13f
    var innerPadding: Int = 10

    var defaultIconTint = android.R.color.black
    var selectedIconTint = android.R.color.white

    var defaultBackgroundTint: Int = android.R.color.white
    var selectedBackgroundTint: Int = android.R.color.black

    var frameBgColor = R.color.shadow
    var textColor = android.R.color.white

GitHub