android-floating-action-button icon indicating copy to clipboard operation
android-floating-action-button copied to clipboard

Toggle Floating Action MENU when clicking outside of it

Open kevinmcampos opened this issue 9 years ago • 10 comments

Anyone knows how to toggle FloatingActionMenu when clicking outside of it? If i'm not clicking in any FAB, I want to close menu. I tried fullscreen overlay onTouchListener but fail.

Thanks!

kevinmcampos avatar Mar 05 '15 00:03 kevinmcampos

Do you mean collapsing the menu when it's expanded but the user touches somewhere outside of it? I set an onTouchListener to my activity layout, with the descendantFocusability set to 'blocksDescendants' and it worked for me.

android:descendantFocusability="blocksDescendants" 

In my Activity, where myLayout is the root layout, and floatingActionsMenu is the floating action menu you want to collapse.

myLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(floatingActionsMenu.isExpanded())
                    floatingActionsMenu.collapse();
                return true;
            }
        });

catorda avatar Mar 07 '15 02:03 catorda

Good to know this catorda! Thanks! This does work if you click on a blank space behind the FAM. Is it possible to prevent clicking of any action buttons that are behind the Floating Action MENU in my main layout?

I have buttons in the layout which get clicked instead of collapsing the FAM menu...

dknchris avatar Mar 07 '15 06:03 dknchris

Have you tried adding a view that overlays the whole screen behind the FAM, and adding the on touch listener to that instead of the root view?

catorda avatar Mar 07 '15 15:03 catorda

@catorda Thanks! Added a child relative layout...works great!

dknchris avatar Mar 08 '15 07:03 dknchris

I think that this kind of behavior should not be baked into the FAM class, what @catorda did is the right approach. This should be a part of sample application though.

chalup avatar May 27 '15 21:05 chalup

Hello, got it working, too :) At thirst I want to thank @catorda for this useful answer.

Here is some code (maybe will help this others, getting this running as well - I wasted some time on this)

The last lines of the xml:

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_action_choose_color"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/white"
            fab:fab_colorPressed="@color/white_pressed"
            fab:fab_size="mini"
            fab:fab_title="@string/fab_action_choose_color"
            />

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_action_choose_symbol"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/white"
            fab:fab_colorPressed="@color/white_pressed"
            fab:fab_size="mini"
            fab:fab_title="@string/fab_action_choose_symbol"
            />

    </com.getbase.floatingactionbutton.FloatingActionsMenu>

    <FrameLayout
        android:id="@+id/fl_interceptor"
        android:descendantFocusability="blocksDescendants"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

And here the coding (note that onTouch returns false if the menu is a collapsed/hidden state)

mInterceptorFrame = (FrameLayout) findViewById(R.id.fl_interceptor);
        mInterceptorFrame.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d(TAG, "onTouch  " + "");
                if (mFabMenu.isExpanded()) {
                    mFabMenu.collapse();
                    return true;
                }
                return false;
            }
        });

Kudos to @chalup for sharing this awesome library with us. Great work!

pepperonas avatar Oct 26 '15 09:10 pepperonas

Thanks, this solved my problem.

Lumuy avatar Mar 11 '16 04:03 Lumuy

I had to put the fl_interceptor FrameLayout before the floating action menu in the XML code snippet from @pepperonas, so that it gets placed below the floating action menu. Otherwise the floating action buttons were not accessible.

mraonghus avatar Aug 23 '16 20:08 mraonghus

@catorda i am using this but my .isExpanded() and .collapse(); both are cannot be resolve please help

hamdeekhan avatar Oct 14 '16 07:10 hamdeekhan

can u make this preview code? please https://s3.amazonaws.com/avantari-assets/Task+4.webp

JakkulaMahendhar avatar Mar 10 '18 18:03 JakkulaMahendhar