6

I'm using the library https://github.com/futuresimple/android-floating-action-button to create the fab buttons that works pretty well. I tried several libraries but none use the effect used in most application with a fab (or Google apps like keep, inbox) that when the fab is clicked the background of entire application becames semi-transparent white or black, it depends. I can't find any way to do it. Is it possible have an example or something like that to do this kind of effect? You can see what i mean in this image: enter image description here

The semi-black background

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/semi_white_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white_semi_transparent"
        android:visibility="gone" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/listFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/content_frame"
        android:visibility="gone" >

        <TextView
            android:id="@+id/status"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/cabinet_color"
            android:fontFamily="sans-serif"
            android:gravity="center_vertical"
            android:paddingBottom="8dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="8dp"
            android:textColor="#fff"
            android:textSize="17sp"
            android:visibility="gone" />

        <android.support.v7.widget.RecyclerView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/status"
            android:clipToPadding="false"
            android:scrollbars="vertical" />

        <LinearLayout
            android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/emptyImage"
                android:layout_width="96dp"
                android:layout_height="96dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="16dp"
                android:scaleType="fitXY"
                android:src="?empty_image" />

            <TextView
                android:id="@+id/emptyText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:fontFamily="sans-serif-light"
                android:gravity="center"
                android:lineSpacingMultiplier="1.4"
                android:paddingBottom="16dp"
                android:text="@string/no_files"
                android:textColor="?empty_text"
                android:textSize="22sp" />
        </LinearLayout>
    </RelativeLayout>

    <ProgressBar
        android:id="@android:id/progress"
        style="?android:progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:indeterminateOnly="true" />

</RelativeLayout>

this is my layout.xml but seems not working at all as i want.Seems that not overlap the entire view but just colors what it wants..

Atlas91
  • 5,754
  • 17
  • 69
  • 141

4 Answers4

5

I found out this to be the easiest solution in my opinion, and it works perfectly :

fam.setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
        @Override
        public void onMenuExpanded() {
            dimmedBackground.setVisibility(View.VISIBLE);
        }

        @Override
        public void onMenuCollapsed() {
            dimmedBackground.setVisibility(View.GONE);
        }
    });
Mehdi
  • 713
  • 9
  • 19
  • awesome nice one i used code from this library https://github.com/Clans/FloatingActionButton and for same work the code is for this library : floatingActionMenu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() { @Override public void onMenuToggle(boolean opened) { displayToast("opened is "+opened); } }); – Khizar Hayat Mar 15 '16 at 09:53
2

Liked your question...

This library won't allow u to set add button click event, you may have to change library code for your own, use this idea: Use Relatie or framelayout and add that black background or image behind FAB.

Now in library class FloatingActionsMenu.class find and comment this code:

//mAddButton.setOnClickListener(new OnClickListener()
//{
//  @Override
//  public void onClick(View v)
//  {
//      toggle();
//  }
//});

Then add this method in that class,

public void setAddButtonClickListener(OnClickListener listener)
{
mAddButton.setOnClickListener(listener);
}

Define click listener in your activity like

OnClickListener listener = new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        if (floatingActionMenuButton.isExpanded())
        {//make the background visible
        }
        else
        {//make the background invisible
        }
        floatingActionMenuButton.toggle();
    }
};

and pass it to FAB

floatingActionMenuButton.setAddButtonClickListener(listener);

Build library and project, Hope this will help!

Harin
  • 2,413
  • 3
  • 15
  • 30
  • Your idea seems corrected.. Just one thing.. as you can see the FloatingActionMenuButton is the "main" fab that when clicked open the other FloatingActionButtons.. If i comment that lines.. It still works? I mean, it will open the other floating buttons or just create the background? I hope i have not decide between them.. I would have both functionallity – Atlas91 Mar 30 '15 at 13:08
  • 1
    floatingActionMenuButton.toggle(); in click listener will open or close other menu buttons and you also can change the background at same time. – Harin Mar 30 '15 at 13:45
  • This is an amazing news. Perfect, as soon as i'm home i'l try your solution. In meanwhile, thank you – Atlas91 Mar 30 '15 at 13:46
  • Well, it works..but not exactly as i want; I can make visibile and invisible the RelativeLayou i created but if i use a semi-white color it colors only the toolbar.. Or if i use semi-black for example, seems that the color not overlaps the recycleview that i have in my layout.. i don't know how could describe – Atlas91 Mar 30 '15 at 17:38
  • I'm going to update the question with the layout so you can see – Atlas91 Mar 30 '15 at 17:39
  • For example; i have a recycleview with a divider for each item. When i try to make visible the semi transparent layou (the black one) The dividers, the icons in the item of the list and the contextual menu of the list becamses more visible. What changes is the color of the list. And the other problem is that i expect that when i tap in the semi transparent area the layout disappear. Is it possible? – Atlas91 Mar 30 '15 at 17:53
  • I haven't worked with RecyclerView but what i can suggest first check this functionality with simple FAB and imageview in main page, If it works properly, try to add RecyclerView. – Harin Apr 01 '15 at 09:07
  • @Harry thanks for your solution, but i have problem with the transparent layer, that fill the parent (all screen) so when "fab" menu is hidden, the transparent layout blocking the previous layer (i cant click on any element from my first layer). In my example I have a layer with a listview and second layer is the "fab" menu. Sorry for my english. – Gaston Flores Jul 10 '15 at 17:29
1

You need to wrap your activity in a FrameLayout. FrameLayout should have 2 childs

  • Your activity content
  • Another Framelayout containing FloatingActionsMenu

Here is the relevant code snippet.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="in.city.bytes.view.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!-- other content in the activity -->
</LinearLayout>

<FrameLayout
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white_overlay">
    <!--  floating action menu with buttons -->
</FrameLayout>

In your activity, change the alpha of frame_layout when FAB menu is clicked. I have also written a blog explaining it. http://www.rishabhsinghal.in/implement-floating-action-button-similar-to-inbox-by-gmail-or-evernote/

Rishabh Singhal
  • 1,173
  • 10
  • 22
0
floatingActionMenu.setOnMenuToggleListener(
  new FloatingActionMenu.OnMenuToggleListener() { 
     @Override 
     public void onMenuToggle(boolean opened) {
         if(opened){
           //show backgroud
         }else{
           //hide backgroud
         }
     }
  });
shashank J
  • 67
  • 7