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
this is the image after scrolling and clicking.
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.
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.
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();
}
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With