Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Floating Actions Menu on click not working

I have an Activity (HomeActivity) with three tabs. Each tab has it own fragment and in one fragment I have Floating Actions Menu with 3 Floating Action Buttons. Here's the XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_home_layout"
android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Recent"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <com.getbase.floatingactionbutton.FloatingActionsMenu
        android:id="@+id/fabMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="16dp"
        android:onClick="fabMenuClicked"
        app:fab_addButtonColorNormal="?attr/colorAccent"
        app:fab_addButtonColorPressed="#00b1c7"
        app:fab_labelStyle="@style/fab_menu_label_style">

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/singleChatFab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:fab_colorNormal="?attr/colorAccent"
            app:fab_colorPressed="#00b1c7"
            app:fab_size="mini"
            app:fab_title="New chat"/>

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/groupChatFab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:fab_colorNormal="?attr/colorAccent"
            app:fab_colorPressed="#00b1c7"
            app:fab_size="mini"
            app:fab_title="New group chat"/>

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/serviceChatFab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:fab_colorNormal="?attr/colorAccent"
            app:fab_colorPressed="#00b1c7"
            app:fab_size="mini"
            app:fab_title="Request a service"/>
    </com.getbase.floatingactionbutton.FloatingActionsMenu>

</RelativeLayout>

It expands on click and it works fine. The thing I want to do is when Floating Action Menu is clicked, change opacity of a layout that holds it (Relative Layout with id: main_home_layout).

But my on click method is never entered. I tried placing it in Activity that holds the fragment, it didn't work. I tried placing in the fragment, still nothing. I ran app in debug mode, break point is never catched.

This is on click method:

public void fabMenuClicked(View v) {
    final FloatingActionsMenu floatingActionsMenu = (FloatingActionsMenu) v;
    findViewById(R.id.main_home_layout).getBackground().setAlpha(128);
}

Does anyone has an idea why it won't work. Thanks.

like image 936
Aleksandar Ivić Avatar asked Jan 31 '26 02:01

Aleksandar Ivić


1 Answers

Try using setOnFloatingActionsMenuUpdateListener, change the color as per your need in overriden onMenuExpanded and onMenuCollapsed method, like :

FloatingActionsMenu floatingMenu = v.findViewById(R.id.fabMenu);             
  ((FloatingActionsMenu)floatingMenu).setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
                    @Override
                    public void onMenuExpanded() {
                        findViewById(R.id.main_home_layout).getBackground().setAlpha(128); //change opacity here
                    }

                    @Override
                    public void onMenuCollapsed() {
                        findViewById(R.id.main_home_layout).getBackground().setAlpha(64); //change opacity here
                    }
            });
like image 60
abhishesh Avatar answered Feb 01 '26 15:02

abhishesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!