Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating Action Button icon image not showing after hide and show

Hi i'm developing an android application with Floating Action Button.

At first time the FAB icon shows icon image. After when i hide and show the icon image will be blank when i click FAB icon

This is the code that is used to hide the FAB

   mainScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                // previousScrollY this variable is define in your Activity or Fragment
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.i(TAG, "onScrollChanged: scrollview position " + mainScrollView.getScrollY() + " " +
                                previousScrollY + " " +
                                mainScrollView.getChildAt(0).getHeight());
                        if (mainScrollView.getScrollY() > previousScrollY) {
                            fab.hide();
                        } else if (mainScrollView.getScrollY() < previousScrollY) {
                            fab.show();
                        }
                        if (mainScrollView.getScrollY() >= mainScrollView.getChildAt(0).getHeight()) {
                            previousScrollY = mainScrollView.getChildAt(0).getHeight();
                        } else if (mainScrollView.getScrollY() < 0) {
                            previousScrollY = 0;
                        } else {
                            previousScrollY = mainScrollView.getScrollY();
                        }
                    }
                }, 200);
            }

        });

onscrolling down the FAB icon will hide and onScrolling up the FAB icon will show.

And this is the code for FAB setOnClicklistener

    fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (sheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
                    sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                    fab.setImageResource(R.drawable.ic_close);
                } else {
                    sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                    fab.setImageResource(R.drawable.ic_filter);
                }
            }
        });

This is the fab icon image that appear Before Scrolling and first time loading Before Scrolling and first time loading opening fab

Before Scrolling and first time loading closing Fab

this is the image after scrolling and clicking.

fab icon image after scrolling and clicking.

like image 939
SoloWolf93 Avatar asked Nov 01 '18 09:11

SoloWolf93


People also ask

How do I display an image in the floating action button?

Add the floating action button to your layout The size of the FAB, using the app:fabSize attribute or the setSize() method. The ripple color of the FAB, using the app:rippleColor attribute or the setRippleColor() method. The FAB icon, using the android:src attribute or the setImageDrawable() method.

How do you make a floating action button invisible?

Use the show and hide methods to animate the visibility of a FloatingActionButton . The show animation grows the widget and fades it in, while the hide animation shrinks the widget and fades it out. Just use: FloatingActionButton fab1 = findViewById(R.


2 Answers

I know it has been a long time since you posted this but I had the same problem. I solved it by doing a hide() and then a show() to the fab after clicking. (In my particular case I change the drawable in onClick. Doing hide() and show() after changing the drawable solved the issue).

Your code shall look like:

fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (sheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
                    sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                    fab.setImageResource(R.drawable.ic_close);
                } else {
                    sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                    fab.setImageResource(R.drawable.ic_filter);
                }
                fab.hide();
                fab.show();
            }
        });
like image 84
Miguel Isla Avatar answered Nov 01 '22 17:11

Miguel Isla


Did face the same issue with dependency com.google.android.material:material:1.0.0. Upgrading the version to 1.1.0-alpha3 fixed the issue.

like image 29
Nishant Avatar answered Nov 01 '22 17:11

Nishant