Floating Action Button Not Going up when Snack bar is Showing in android

Kumar picture Kumar · Mar 14, 2016 · Viewed 11k times · Source
  1. I am Using Snack bar and FAB in my application Same Page,Whenever Snackbar is Showing Floating Action button not Going up.

  2. I am Using Third Party library for attachToListView works fine

     import com.melnykov.fab.FloatingActionButton;
    

if I am using Default library "cannot be resolved attachToListView"

import android.support.design.widget.FloatingActionButton;

My Need:

  1. attachToListView Should Work(For When Listview Scroling Down FAB will be Disappear).

  2. Whenever Snackbar is Showing Floating Action button Should Go up.

Help me How to Solve this Issue.

Third Party Library Link

EDIT :1

I Removed Third Party Library added Default Import (import android.support.design.widget.FloatingActionButton),FAB is Going Up but Attachtolistivew not Resolved.

EDIT :2

I Used Listview In my activity ,with FAB and Snackbar. So i need Both Options Like FAB Go up When Snackbar Opens and when Listview is Scrolling down Should hide FAB.

My SnackBar Code:

 Snackbar snack = Snackbar.make(fab1, " Successfully ...!",Snackbar.LENGTH_SHORT);
                    View snackbarView = snack.getView();
                    snackbarView.setBackgroundColor(Color.parseColor("#f44336"));
                    snack.show();

Main.java

import com.melnykov.fab.FloatingActionButton;
     @Override
        protected void onCreate(Bundle savedInstanceState) 
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fabview);
    
            fab1 = (FloatingActionButton) findViewById(R.id.fab);
    
            fab1.setShadow(true);
            //fab.attachToListView(provider_service_list);
    
            //FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab1.attachToListView(listViewData, new ScrollDirectionListener()
            {
                @Override
                public void onScrollDown() {
                    Log.d("ListViewFragment", "onScrollDown()");
                }
    
                @Override
                public void onScrollUp() {
                    Log.d("ListViewFragment", "onScrollUp()");
                }
            }, new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    Log.d("ListViewFragment", "onScrollStateChanged()");
                }
    
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    Log.d("ListViewFragment", "onScroll()");
                }
            });
    
    }

fabview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app78="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    >
    <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/coordinatorlayout">
    
            <com.melnykov.fab.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|right"
                android:layout_margin="@dimen/fab_margin"
                android:src="@drawable/ic_add_white_24dp"
                app78:fab_colorNormal="@color/accent"
                app78:fab_colorPressed="@color/accent_pressed"
                app78:fab_colorRipple="@color/ripple"
                app78:fabSize="normal"
                app78:borderWidth="0dp"
                android:layout_marginBottom="@dimen/fab_margin_bottom"
                android:layout_marginRight="@dimen/fab_margin_right"
                />
    
        </android.support.design.widget.CoordinatorLayout>
   </RelativeLayout>

Answer

user5703518 picture user5703518 · Mar 18, 2016

Your requirements are:

1)When Listview Scroling Down FAB will be Disappear

2)Whenever Snackbar is Showing Floating Action button Should Go up.

Now examine your solutions:

1) If you are going to use design support library fab you will only satisfy constraint number 2.

2) If you are going to use third party fab you will only satisfy constraint number 1.

You can satisfy your 2 constraints by either manipulating third party library and adding animation when you are going to show snackbar or by manipulating ListView to put it inside CordinateLayout and then make fab disappear when ListView is Scrolling down.

My solution:

Fastest solution is changing your ListView to RecyclerView and using CordinateLayout and design support library fab. now you can achieve #2. (RecyclerView is more powerful widget than ListView. Why do I have to do that? because CordinateLayout creates your animation and it works only by any scrolling widget that implements NestedScrolling interface. the ListView from LOLLIPOP and up implements that interface. if your min SDK is lower than LOLLIPOP you have to use RecyclerView).

for number #1 you can use below code:

 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (dy > 0 && fab.isShown())
                    fab.hide();
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                if (newState ==  RecyclerView.SCROLL_STATE_IDLE) {
                    fab.show();
                }
                super.onScrollStateChanged(recyclerView, newState);
            }
        }); 

your layout would be:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="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=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways|snap"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>